Reputation: 15
I've got a JSON Request that I'm able to add to an NSDictionary but I'm unable to access part of the JSON string. Here is a sample
{
"Resp": {
"Success": "True",
"who": {
"userid": 234,
},
"students": [
{
"ID": 1,
"name": John
},
{
"ID": 2,
"name": Jane
}
],
}
}
I am storing the data in an NSMutableDictionary and then passing that to my function which should run through each student and process them accordingly.
Here is what I've got so far and it's not working:
-(void)foo:(NSMutableDictionary*)json {
NSArray *students = json[@"students"];
for(NSMutableDictionary *student in students) {
NSLog(@"student id: %@", student[@"ID"]);
}
}
When debugging I can see the JSON object and students belongs under Resp as a value.
Upvotes: 0
Views: 101
Reputation: 510
First of all, check your JSON on the validity http://jsonlint.com . It corrected your JSON with next modifications:
{
"Resp": {
"Success": "True",
"who": {
"userid": 234
},
"students": [
{
"ID": 1,
"name": "John"
},
{
"ID": 2,
"name": "Jane"
}
]
}
}
Then you should access your elements in right way:
- (void)foo:(NSMutableDictionary *)json {
NSArray *students = json[@"Resp"][@"students"];
for(NSDictionary *student in students) {
NSLog(@"student id: %@", student[@"ID"]);
}
}
For now it should be working.
Upvotes: 1