I’m having an issue parsing JSON from a PHP server using NSJSONSerialization. JSLint says my JSON is valid but appears to only be able to get one-two levels in.
This is essentially my JSON structure:
{
"products":
[{
"product-name":
{
"product-sets":
[{
"set-3":
{
"test1":"test2",
"test3":"test4"
},
"set-4":
{
"test5":"test6",
"test7":"test8"
}
}]
},
"product-name-2":
{
"product-sets":
[{
}]
}
}]
}
and here is my code to parse it:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if (json) {
NSArray *products = [json objectForKey:@"products"]; // works
for (NSDictionary *pItem in products) { // works
NSLog(@"Product: %@", pItem); // works, prints the entire structure under "product-name"
NSArray *productSets = [pItem objectForKey:@"product-sets"]; // gets nil
for (NSDictionary *psItem in productSets) {
// never happens
}
}
}
I’ve been spinning my wheels on this for several hours, but I’m not finding anything similar anywhere I search. Are there any limitations that I’m unaware of, or am I just not seeing something obvious?
you missed one nested object
I tested it with this CLI program
Note, that some things in your json are quite strange:
for each flattened object the keys should be the same. keys, that include a number o an object do not make much sense. If you need to keep track of single objects, include an id key with a proper value.