1

I am adding key value strings to an NSArray, but they are added in the wrong order, which leads me to believe that this is the wrong way to do things-

my json takes the following format:

[{
    "story1":
        {data in here}
    "story2":
                {data in here}....

and in my code I am wishing to get the string values of story1 and story2 (and more) in an NSArray, which I achieve but they are in the opposite order - story2, story1:

NSArray *jsonArr = [NSJSONSerialization JSONObjectWithData:theData options:kNilOptions error:&error];

for (NSString *pItem in [jsonArr objectAtIndex:0]) {
  NSLog(@"Product: %@", pItem);
}

is there a better way to do this, and if not, how can i reverse the array?

5
  • 4
    You're not adding them to an array, you're adding them to a dictionary inside an array. The elements of a dictionary are unordered. Commented Nov 26, 2012 at 0:12
  • 3
    ([..] is a JSON array. {..} is a JSON "object", which is essentially the same as a Objective-C NSDictionary. By definition (the JSON spec), elements of an "object" are unordered.) Commented Nov 26, 2012 at 0:13
  • you can always sort the keys from the dictionary. Though, that only works for keys like those in the example. If there is an arbitrary order you should figure out why the creator of the JSON didn't use an array in thr first place. Commented Nov 26, 2012 at 0:27
  • Thanks for the comments! The JSON is my own creation - I thought the story1, story2 etc were already in an array? as they are enclosed by [] or am I missing something? the {data in here} i know is an object and isn't required by me at this stage. Commented Nov 26, 2012 at 1:01
  • 1
    They are most closely enclosed by {}. The dictionary is in an array, but it's kind of a degenerate case of an array with only one element. (Study the JSON spec to understand this.) Commented Nov 26, 2012 at 1:47

2 Answers 2

1

Your JSON defines a JSON object inside a JSON array. When you deserialise it, you are getting an NSArray containing an NSDictionary which, in turn, contains the key value pairs, but NSDictionary does not define an ordering for the keys (nor does a JSON object). i.e.

{ "story1" : "foo", "story2" : "bar" }

and

{ "story2" : "bar", "story1" : "foo" }

are repesentations of the same object.

If you need an ordering, you need to restructure your JSON data. The follwoing will do the trick

[
    { "story1" : { ... } },
    { "story2" : { ... } }
]

Alternatively, when you access the data in your objects, you can sort the keys first. Using your example:

NSArray *jsonArr = [NSJSONSerialization JSONObjectWithData:theData ...];
NSDictionary *jsonDictionary = [jsonArr objectAtIndex: 0];

NSArray* sortedKeys = [[jsonDictionary allKeys] sortedArrayUsingComparator: (^NSComparator)(id obj1, id obj2) { /* a comparator */ }];
for (NSString *key in [sortedKeys]) 
{
    NSLog(@"Product: %@", [jsonDictionary objectForKey: key]);
}
Sign up to request clarification or add additional context in comments.

Comments

1
// Instead Normal NSArray user NSOrderedSet which preserves the order of objects
NSOrderedSet *orderSet = [NSOrderedSet orderedSetWithArray:jsonArr];

// Access your value From NSOrderedSet same as NSArray
NSDictionary *dict = [orderSet objectAtIndex:0];
NSLog(@"%@",dict);

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.