0

I am using JSON Parsing in my application. My problem is when I get my response String from server , I parsed that string through JSON Parser and data in my dictionary. Parsing goes fine, but the ordering did not get maintained means when my string come it has "Learning Chinese" value first but when parsing that and getting data into dictionary then it did not come on first element.

I would like to explain this with my example code: String which I want to parse:

NSString *respStr = [req responseString];
NSLog(@"respStr=%@",respStr);


{
  "Learning Chinese": [
    {
      "title": "Chinese nga28Traditionalnga27 Audio FlashCards for iPad",
      "function_name": "Learning Chinese"
    },
    {
      "title": "WCC Chinese Flashcards nga28Bigramnga27 with Audio",
      "function_name": "Learning Chinese"
    }
  ],
  "Business & Office": [
    {
      "title": "Instant Customer",
      "function_name": "Business & Office"
    },
    {
      "title": "Phone Swipe nga7 Credit Card Terminal",
      "function_name": "Business & Office"
    }
  ],
  "Kids": [
    {
      "title": "iWriteWords nga28Handwriting Gamenga27",
      "function_name": "Kids"
    },
    {
      "title": "SoundBook nga25 audio flashcards for toddlers",
      "function_name": "Kids"
    }
  ],
  "Christmas": [
    {
      "title": "Santanga2s Fun",
      "function_name": "Christmas"
    },
    {
      "title": "Santerrific",
      "function_name": "Christmas"
    }
  ]
}

Now

NSMutableDictionary *dict=[respStr JSONValue];
NSLog(@"dict=%@",dict);

I got:

dict={
    "Business & Office" =     (
                {
            "function_name" = "Business & Office";
            title = "Instant Customer";
        },
                {
            "function_name" = "Business & Office";
            title = "Phone Swipe nga7 Credit Card Terminal";
        }
    );
    Christmas =     (
                {
            "function_name" = Christmas;
            title = "Santanga2s Fun";
        },
                {
            "function_name" = Christmas;
            title = Santerrific;
        }
    );
    Kids =     (
                {
            "function_name" = Kids;
            title = "iWriteWords nga28Handwriting Gamenga27";
        },
                {
            "function_name" = Kids;
            title = "SoundBook nga25 audio flashcards for toddlers";
        }
    );
    "Learning Chinese" =     (
                {
            "function_name" = "Learning Chinese";
            title = "Chinese nga28Traditionalnga27 Audio FlashCards for iPad";
        },
                {
            "function_name" = "Learning Chinese";
            title = "WCC Chinese Flashcards nga28Bigramnga27 with Audio";
        }
    );
}

After that:

NSLog(@"[dict allKeys]=%@",[dict allKeys]);

[dict allKeys]=(
    Christmas,
    Kids,
    "Learning Chinese",
    "Business & Office"
)

From the code it is clear that in string Learning Chinese is first but in dictionary it is on 4 number and when doing allKeys it comes on 3 number.

Please if anybody know why is this happening,tell me or any other way of solving it.Ordering is necessary for me.I have to show my data in the same order as in the string which is returned from server.

Any suggestions will be highly appreciated.

Thanks in advance.

2

1 Answer 1

1

You have to know that keys in NSDictionary/NSMutableDictionary are not ordered. To solve this you can either use an array or sort it by yourself in your code.

Your question is very similar to this one: Objective-C SBJSON: order of json array

Sign up to request clarification or add additional context in comments.

1 Comment

thanks Rafal for your answer+1.Yes I saw that question also you referred but it did not solved my problem.

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.