Say you do this,
NSString *teste = yourData[@"title"];
no problem if "title"
is completely missing in the json: you just get null
. If you do this:
NSString *teste = yourData[@"location"][@"city"];
if "city"
is missing in the json nest, no problem. if the whole "location"
section does not exist, again no problem
However! You'll often see json like this, " largeImage = "<null>"; "
In that case, the app will crash if you are using the code above.
In practice you have to do this:
NSString *imageUrl = nil;
if ([yourResults[thisRow][@"largeImage"] isKindOfClass:[NSDictionary class]])
imageUrl = yourResults[thisRow][@"largeImage"][@"URL"];
My question was really:
is there some dead clever way to perhaps override the literal syntax (ie, override the underlying message, perhaps??) to cover this problem?
So essentially, make this concept [@"blah"]
basically first check that indeed it is a dictionary at hand, before trying the operation.
It's a shame because, effectively, you can never use this wonderful syntax
yourData[@"location"][@"city"]
in practice, due to the problem I outline.
PS sorry for the earlier confusion on this question, fixed by Paramag. below - good one Paramag.
Personally i use with JSON category which returns null instead NSNull so my code looks:
As you want to have code shorter, i think i would create the category on NSDictionary which could be used as :
Which would expand the path into the keys.
There is some nice gist which looks like it's doing it:
https://gist.github.com/Yulong/229a62c1188c3c024247#file-nsdictionary-beeextension-m-L68