1

Trying to parse this JSON object in objective-C and creating an NSArray with these objects. The first value is a counter and is specific for the object. All other values are unique.

{ "myData": [
["1","1","110","dollar","8.0","2.8","0.1","11.6"],
["2","1","110","euro","4.0","3.2","1.5","4.4"],
["3","1","120","rupier","6.0","2.9","1.3","10.8"],
["4","1","120","dinero","4.0","3.3","1.5","4.4"],
["5","2","130","drahmer","8.0","2.9","1.3","11.2"],
] }

Tried this code:

 NSDictionary* json = [NSJSONSerialization 
                          JSONObjectWithData:myData
                          options:kNilOptions 
                          error:&error];

    NSArray *currencyInformation = [json objectForKey:@"myData"];

But the objects are not there. Though the count of the array is 5.

5
  • 3
    if [array count] is 5 as expected, the objects are there. How do you know they aren't? Commented Mar 18, 2012 at 20:59
  • Can´t read them from the array. Since they don't have any description for the value, ie. "currency": "dollar" I can't ready them out... Commented Mar 18, 2012 at 21:04
  • Where is currency? I don't see that in the JSON at all. Show us how you're accessing the values in the array. (Hint: they will be NSArrays themselves). Commented Mar 18, 2012 at 21:20
  • @mattjgalloway: There you got it. Every line is an array in it self. When handling it like an array it solved the problem. Thanks. Commented Mar 18, 2012 at 21:30
  • @linge - Well I've added an answer to say that :-). Glad it helped. Commented Mar 18, 2012 at 21:33

2 Answers 2

2

Each object in the array is an array itself, so:

NSDictionary* json = [NSJSONSerialization 
                      JSONObjectWithData:myData
                      options:kNilOptions 
                      error:&error];

NSArray *currencyInformation = [json objectForKey:@"myData"];

for (NSArray *info in currencyInformation) {
    // Then access each "column" with [info objectAtIndex:0,1,2,3,...]
}
Sign up to request clarification or add additional context in comments.

Comments

1

In this data structure you would need to access things by index e.g

for (NSArray *currency in currencyInformation) {
    NSLog(@"Currency: %@", [currency objectAtIndex:3]);
}

If you want to access things by key then you would need to change your JSON to use an array of objects instead of an array of arrays. Something like this:

{ 
    "myData": [
        {
            "primaryKey" : 1,
            "currency"   : "dollar",
            <other keys + values>...
        },
    ]
}

In which case you could now do something like:

for (NSDictionary *currency in currencyInformation) {
    NSLog(@"Currency: %@", [currency valueForKey:@"currency"]);
}

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.