How to keep the original order when read the Json string about IOS development? -


as title suggests, meet question when read json string dictionary, dictionary order different json string original order. there knows how solve it? maybe tell me dictionary disorderly, however, our project needs sort dictionary json string original order.

a json object define in json spec unordered collection.

an object unordered set of name/value pairs. (http://json.org/)

here possible solutions:

  1. change json object array of objects. if control server far best option. if order important array right choice.
  2. send order along json. can have array of keys included in json. again, need have control of server this.
  3. sort keys after them. can work if order want can derived keys. example if keys alphabetical or numbers. if control keys (even if don't control entire server response) can hack change keys sortable in way works (for example change keys ["stores","locations"] ["0_stores","1_locations"]. can work if know possible keys , order.
  4. order keys location in json dictionary. if keys in json guaranteed not show else in json (i.e. there no arbitrary text field in json) can dictionary keys sorted looking appear in json string:

    nsarray* sortedkeys = [[dictionary allkeys] sortedarrayusingcomparator:^nscomparisonresult(nsstring*  key1, nsstring* key2) {         nsinteger location1 = [jsonstring rangeofstring:key1].location;         nsinteger location2 = [jsonstring rangeofstring:key2].location;         return [@(location1) compare:@(location2)]; }]; 
  5. if none of these options work have find or create own json parser respects order. think hard find such library, since explicitly against json spec. not recommend option.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -