开发者

JSON Sub Level Extraction - How to? Objective-C

I'm using SBJSON (and am new to JSON) and can easily extract httprequest's response data with 开发者_运维问答it at its top most level. The problem is that I am quite clueless on accessing elements in a sub level.

For instance:

Level 1:

Key: id

Key: car_list

=========

car_list (Level 2)

Key: car_id

Key: car_name

Key: car_size

So like I said, I can access top level objects by using:

NSDictionary *responseDict = [responseString JSONValue];
NSString *id = [responseDict objectForKey:@"id"];

But don't know how to access the elements within the "car_list".

Thanks


A simple way, if you have a dictionary of dictionaries, is to use valueForKeyPath:

For example:

NSString *carName = [responseDict valueForKeyPath:@"car_list.car_name"];

All you need to do is join up your keys with periods. If any of those paths returns an array or set, then you'll receive a collection. You can additionally use set/array key-value coding paths to compute things on collections, such as @avg and @count.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜