Prettiest way to parse JSON in Objective-C?

ios, json, macos, objective-c

Solution

You could use the dictionary literals:

[[self getInformationFrom:mtgox][@"data"][@"last"][@"value"] floatValue];

Or you could use KVC:

[[[self getInformationFrom:mtgox] valueForKeyPath:@"data.last.value"] floatValue];

Problem

I am trying to parse some JSON from an API, but I have 'trouble' parsing sub-elements of the JSON. My methods of doing that seems like it could be prettier. I am trying to parse from `http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast?pretty` and I get ``` { "result": "success", "data": { "last_local": { "value": "785.00000", "value_int": "78500000", "display": "$785.00", "display_short": "$785.00", "currency": "USD" }, "last": { "value": "785.00000", "value_int": "78500000", "display": "$785.00", "display_short": "$785.00", "currency": "USD" }, "last_orig": { "value": "785.00000", "value_int": "78500000", "display": "$785.00", "display_short": "$785.00", "currency": "USD" }, "last_all": { "value": "785.00000", "value_int": "78500000", "display": "$785.00", "display_short": "$785.00", "currency": "USD" }, "buy": { "value": "785.00000", "value_int": "78500000", "display": "$785.00", "display_short": "$785.00", "currency": "USD" }, "sell": { "value": "785.50000", "value_int": "78550000", "display": "$785.50", "display_short": "$785.50", "currency": "USD" }, "now": "1386421846023718" } } ``` Now I want to access `value`at `last` at `data`. My way of doing this is nesting a bunch of objectForKey:string -- such as `[[[[[self getInformationFrom:mtgox] objectForKey:@"data"] objectForKey:@"last"] objectForKey:@"value"] floatValue];` Basically what `getInformationFrom:mtgox` does is return an NSDictionary with NSJSONSerialization. Is there a better way of getting the value of this JSON data?

Original source