Objective-C
Objective-C
Modify Parts of JSON Document
See more JSON Examples
Demonstrates how to modify parts of a JSON document. This example uses the following JSON document:
{
"fruit": [
{
"kind": "apple",
"count": 24,
"fresh": true,
"extraInfo": null,
"listA": [ "abc", 1, null, false ],
"objectB": { "animal" : "monkey" }
},
{
"kind": "pear",
"count": 18,
"fresh": false,
"extraInfo": null
"listA": [ "xyz", 24, null, true ],
"objectB": { "animal" : "lemur" }
}
],
"list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ],
"alien" : true
}
Chilkat Objective-C Downloads
#import <CkoJsonObject.h>
#import <CkoJsonArray.h>
BOOL success = NO;
CkoJsonObject *json = [[CkoJsonObject alloc] init];
// Load the JSON from a file.
success = [json LoadFile: @"qa_data/json/modifySample.json"];
if (success == NO) {
NSLog(@"%@",json.LastErrorText);
return;
}
// This example will not check for errors (i.e. null / false / 0 return values)...
// Get the "list" array:
CkoJsonArray *listA = [[CkoJsonArray alloc] init];
[json ArrayOf2: @"list" jarr: listA];
// Modify values in the list.
// Change banana to plantain
success = [listA SetStringAt: [NSNumber numberWithInt: 0] value: @"plantain"];
// Change 12 to 24
success = [listA SetIntAt: [NSNumber numberWithInt: 1] value: [NSNumber numberWithInt: 24]];
// Change true to false
success = [listA SetBoolAt: [NSNumber numberWithInt: 2] value: NO];
// Is the 3rd item null?
BOOL bNull = [listA IsNullAt: [NSNumber numberWithInt: 3]];
// Change "orange" to 32.
success = [listA SetIntAt: [NSNumber numberWithInt: 4] value: [NSNumber numberWithInt: 32]];
// Change 12.5 to 31.2
success = [listA SetNumberAt: [NSNumber numberWithInt: 5] value: @"31.2"];
// Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
// Do this by deleting, then inserting a new object at the same location.
success = [listA DeleteAt: [NSNumber numberWithInt: 6]];
CkoJsonObject *tickerObj = [[CkoJsonObject alloc] init];
[listA AddObjectAt2: [NSNumber numberWithInt: 6] json: tickerObj];
success = [tickerObj AppendString: @"ticker" value: @"GOOG"];
// Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
success = [listA DeleteAt: [NSNumber numberWithInt: 7]];
CkoJsonArray *aa = [[CkoJsonArray alloc] init];
[listA AddArrayAt2: [NSNumber numberWithInt: 7] jarr: aa];
success = [aa AddStringAt: [NSNumber numberWithInt: -1] value: @"apple"];
success = [aa AddIntAt: [NSNumber numberWithInt: -1] value: [NSNumber numberWithInt: 22]];
success = [aa AddBoolAt: [NSNumber numberWithInt: -1] value: YES];
success = [aa AddNullAt: [NSNumber numberWithInt: -1]];
success = [aa AddNumberAt: [NSNumber numberWithInt: -1] numericStr: @"1080.25"];
// Get the "fruit" array
CkoJsonArray *aFruit = [[CkoJsonArray alloc] init];
[json ArrayAt2: [NSNumber numberWithInt: 0] jarr: aFruit];
// Get the 1st element:
CkoJsonObject *appleObj = [[CkoJsonObject alloc] init];
[aFruit ObjectAt2: [NSNumber numberWithInt: 0] jsonObj: appleObj];
// Modify values by member name:
success = [appleObj SetStringOf: @"fruit" value: @"fuji_apple"];
success = [appleObj SetIntOf: @"count" value: [NSNumber numberWithInt: 46]];
success = [appleObj SetBoolOf: @"fresh" value: NO];
success = [appleObj SetStringOf: @"extraInfo" value: @"developed by growers at the Tohoku Research Station in Fujisaki"];
// Modify values by index:
CkoJsonObject *pearObj = [[CkoJsonObject alloc] init];
[aFruit ObjectAt2: [NSNumber numberWithInt: 1] jsonObj: pearObj];
success = [pearObj SetStringAt: [NSNumber numberWithInt: 0] value: @"bartlett_pear"];
success = [pearObj SetIntAt: [NSNumber numberWithInt: 1] value: [NSNumber numberWithInt: 12]];
success = [pearObj SetBoolAt: [NSNumber numberWithInt: 2] value: NO];
success = [pearObj SetStringAt: [NSNumber numberWithInt: 3] value: @"harvested in late August to early September"];
json.EmitCompact = NO;
NSLog(@"%@",[json Emit]);