简体   繁体   中英

parsing JSON response from server in same order in ios

i have an app which fetches JSON response from server. the JSON response from server looks as follows:

{"status":"SUCCESS","message":"XYZ","token":"ABCDEFGHIJ"}

now i need to store this in a NSDictionary for further parsing. so i use the following approach:

    urldata1=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&res   error:nil]; NSDictionary
    *myDictionary=[NSJSONSerialization JSONObjectWithData:urldata1 options:NSJSONReadingMutableContainers error:nil];
 }

but now the dictionary i get looks as follows:

{
    message = "XYZ";
    status = SUCCESS;
    token = "ABCDEFGHIJ";
}

So i see that the dictionary has been sorted on the basis of keys... is there a way to reproduce the exact same response from server in my dictionary..

It doesn't matter in what order the NSDictionary is because you retrieve the object from the dictionary with keys. So if you want to access the status first use this code

NSString *status  = [myDictionary objectForKey@"status"];
NSString *message = [myDictionary objectForKey@"message"];
NSString *token = [myDictionary objectForKey@"token"];

And you can access a Dictionary inside a Dictionary like this

NSDictionary *dict= [myDictionary objectForKey@"SomeOtherDictionary"];

Sorting a dictionary is meaningless. You need to first create an array which will sort according to your needs.

You can refer Sorting NSDictionary from JSON for UITableView for further explanations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM