2

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..

1
  • if u want to get order wise form server then from server side u have to change dictionary key/value pair as array of dictionaries....like that [{"key":"value"},{"key":"value'},..] Commented May 26, 2016 at 9:30

2 Answers 2

1

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"];
Sign up to request clarification or add additional context in comments.

5 Comments

i know we retrieve it using keys but in my case i need to reproduce the dictionary in similar way as i am getting from server. because this dictionary contains a sub dictionary and i need the keys in same order as the received response from server.
@ArpitRaniwala can you please post the full code you receive from server?
{"status":"SUCCESS","message":"NA","users":{"171":{A Dictionary inside},"20":{A Dictionary inside},"110":{A Dictionary inside},"8":{A Dictionary inside},"16":{A Dictionary inside}},"totalCount":41} But after applying JSON serialization the dictionary looks like follows: {"status":"SUCCESS","message":"NA","users":{"110":{A Dictionary inside},"16":{A Dictionary inside},"171":{A Dictionary inside},"2":{A Dictionary inside},"8":{A Dictionary inside}},"totalCount":41}
i have posted the response... Will be waiting for ur reply.
+1. Dictionaries are unordered in their nature. I'm guessing you want to access the "users". Try something like [[myDictionary objectForKey:@"users"] allKeys]; to get an array of all the user IDs. From there you can sort the array, then use [[myDictionary objectForKey:@"users"] objectForKey:userID]; to get the dictionary for THAT user. How you want to sort that array is up to you of course.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.