Question

I was doing pretty good parsing YELP1 Json with NSJSONSerialization and accessing the data with NSDictionary. However, I figured inconsistencies in the response:

Some keys are surrounded with " , mostly the ones containing a _

Parts from the response:

(
        {
        address1 = "xxx";
        address2 = "";
        address3 = "";
        "avg_rating" = 5;
        city = "Chicago";
        country = USA;
        "country_code" = US;

Obtained from
[[NSString alloc] initWithData:self.recievedData encoding:NSUTF8StringEncoding];

That's inconsistent to the documentation where no keys are in brackets. http://www.yelp.com/developers/documentation/search_api

Aperently, the brackets indicate the '_' in the key value for some reason, not the type of the value. Brackets surround each key containing _ , may the value be a string or a int or whatever.

Accessing fields such as avg_rating with

[dict objectForKey:@"avg_rating"];

fails with NSInvalidArgumentException

while [dict objectForKey:@"country"]; would work.

Hence, I dont believe it's a NSDictionary / NSLog problem with the value returned, but a problem with the key submitted to the dict object. Or, maybe during the JSON parsing? But please, prove me wrong with piece of working code... :)

Any ideas? Thanks, el

Edit: edited because question might now have been clear enough.
Edit2: In fact, it was that a NSNumber is returned here. Got confused about these "" .

Was it helpful?

Solution

Parts from the response:

No. That's not part of the response. That's the description of the resulting NSDictionary which is NOT JSON.

NSLog([dict objectForKey:@"avg_rating"]); fails with NSInvalidArgumentException, while NSLog([dict objectForKey:@"country"]); would work.

Sure, because NSLog() ain't no magic. It expects a (format) string as its first argument, so if the object you feed it is an NSString, it will work fine (country is a string, I assume), but if you don't (avg_rating may be an NSNumber), then it won't (it will try to send the messages of NSString to the NSNumber instance, which obviously fails).

So understand the difference between JSON and the description of the NSDictionary generated from that JSON.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top