Objective-C
Objective-C
JSON: Nested Objects
See more JSON Examples
Here we have a JSON object that contains nested JSON objects. This example demonstrates how to access the contents of the nested objects.
{
"name": "donut",
"image":
{
"fname": "donut.jpg",
"w": 200,
"h": 200
},
"thumbnail":
{
"fname": "donutThumb.jpg",
"w": 32,
"h": 32
}
}
Chilkat Objective-C Downloads
#import <CkoJsonObject.h>
#import <NSString.h>
BOOL success = NO;
CkoJsonObject *json = [[CkoJsonObject alloc] init];
// This is the above JSON with whitespace chars removed (SPACE, TAB, CR, and LF chars).
// The presence of whitespace chars for pretty-printing makes no difference to the Load
// method.
NSString *jsonStr = @"{\"name\": \"donut\",\"image\":{\"fname\": \"donut.jpg\",\"w\": 200,\"h\": 200},\"thumbnail\":{\"fname\": \"donutThumb.jpg\",\"w\": 32,\"h\": 32}}";
success = [json Load: jsonStr];
if (success == NO) {
NSLog(@"%@",json.LastErrorText);
return;
}
// Get the "image" object.
CkoJsonObject *imageObj = [[CkoJsonObject alloc] init];
[json ObjectOf2: @"image" jsonObj: imageObj];
NSLog(@"%@%@%@%d%@%d",@"image: fname=",[imageObj StringOf: @"fname"],@", width=",[[imageObj IntOf: @"w"] intValue]
,@", height=",[[imageObj IntOf: @"h"] intValue]);
// Get the "thumbnail" object.
CkoJsonObject *thumbObj = [[CkoJsonObject alloc] init];
[json ObjectOf2: @"thumbnail" jsonObj: thumbObj];
NSLog(@"%@%@%@%d%@%d",@"thumbnail: fname=",[thumbObj StringOf: @"fname"],@", width=",[[thumbObj IntOf: @"w"] intValue]
,@", height=",[[thumbObj IntOf: @"h"] intValue]);