1 Preface
Use the jsonobjectwithdata: Options: Error: Method of the nsjsonserialization class to parse JSON data and store it in the data or dictionary.
2 Sample Code
Testdemo. m
[Plain]
-(Void) convsefromjson {
Nsmutabledictionary * dictionary = [[nsmutabledictionary alloc] init];
[Dictionary setvalue: @ "Anthony" forkey: @ "first name"];
[Dictionary setvalue: @ "Robbins" forkey: @ "last name"];
[Dictionary setvalue: [nsnumber numberwithunsignedinteger: 51] forkey: @ "Age"];
Nsarray * arrayofanthonyschildren = [[nsarray alloc] initwithobjects:
@ "Anthony's son 1 ",
@ "Anthony's daughter 1 ",
@ "Anthony's son 2 ",
@ "Anthony's son 3 ",
@ "Anthony's daughter 2", nil];
[Dictionary setvalue: arrayofanthonyschilpolicforkey: @ "children"];
Nserror * error = nil;
Nsdata * jsondata = [nsjsonserialization
Datawithjsonobject: dictionary options: nsjsonwritingprettyprinted error: & error];
If ([jsondata length]> 0 & error = nil ){
Nslog (@ "successfully serialized the dictionary into data .");
/* Convert JSON to array/dictionary */
Error = nil;
// Conversion Method
Id jsonobject = [nsjsonserialization
Jsonobjectwithdata: jsondata options: nsjsonreadingallowfragments
Error: & error];
If (jsonobject! = Nil & error = nil ){
Nslog (@ "successfully deserialized ...");
// If jsonobject is a dictionary class
If ([jsonobject iskindofclass: [nsdictionary class]) {
Nsdictionary * deserializeddictionary = (nsdictionary *) jsonobject;
Nslog (@ "dersialized JSON dictionary =%@", deserializeddictionary );
}
// If jsonobject is an array class
Else if ([jsonobject iskindofclass: [nsarray class]) {
Nsarray * deserializedarray = (nsarray *) jsonobject;
Nslog (@ "dersialized JSON array = % @", deserializedarray );
} Else {
Nslog (@ "I can't deal with it ");
}
}
Else if (error! = Nil ){
Nslog (@ "an error happened while deserializing the JSON data .");}
}
Else if ([jsondata length] = 0 & error = nil ){
Nslog (@ "no data was returned after serialization .");
}
Else if (error! = Nil ){
Nslog (@ "an error happened = % @", error );
}
}
-(Void) convsefromjson {
Nsmutabledictionary * dictionary = [[nsmutabledictionary alloc] init];
[Dictionary setvalue: @ "Anthony" forkey: @ "first name"];
[Dictionary setvalue: @ "Robbins" forkey: @ "last name"];
[Dictionary setvalue: [nsnumber numberwithunsignedinteger: 51] forkey: @ "Age"];
Nsarray * arrayofanthonyschildren = [[nsarray alloc] initwithobjects:
@ "Anthony's son 1 ",
@ "Anthony's daughter 1 ",
@ "Anthony's son 2 ",
@ "Anthony's son 3 ",
@ "Anthony's daughter 2", nil];
[Dictionary setvalue: arrayofanthonyschilpolicforkey: @ "children"];
Nserror * error = nil;
Nsdata * jsondata = [nsjsonserialization
Datawithjsonobject: dictionary options: nsjsonwritingprettyprinted error: & error];
If ([jsondata length]> 0 & error = nil ){
Nslog (@ "successfully serialized the dictionary into data .");
/* Convert JSON to array/dictionary */
Error = nil;
// Conversion Method
Id jsonobject = [nsjsonserialization
Jsonobjectwithdata: jsondata options: nsjsonreadingallowfragments
Error: & error];
If (jsonobject! = Nil & error = nil ){
Nslog (@ "successfully deserialized ...");
// If jsonobject is a dictionary class
If ([jsonobject iskindofclass: [nsdictionary class]) {
Nsdictionary * deserializeddictionary = (nsdictionary *) jsonobject;
Nslog (@ "dersialized JSON dictionary =%@", deserializeddictionary );
}
// If jsonobject is an array class
Else if ([jsonobject iskindofclass: [nsarray class]) {
Nsarray * deserializedarray = (nsarray *) jsonobject;
Nslog (@ "dersialized JSON array = % @", deserializedarray );
} Else {
Nslog (@ "I can't deal with it ");
}
}
Else if (error! = Nil ){
Nslog (@ "an error happened while deserializing the JSON data .");}
}
Else if ([jsondata length] = 0 & error = nil ){
Nslog (@ "no data was returned after serialization .");
}
Else if (error! = Nil ){
Nslog (@ "an error happened = % @", error );
}
}
Console result
17:26:15. 726 fromjsontest [4944: 303] successfully serialized the dictionary into data.
17:26:15. 728 fromjsontest [4944: 303] successfully deserialized...
17:26:15. 728 fromjsontest [4944: 303] dersialized JSON dictionary = {
Age = 51;
"First name" = ANTHONY;
"Last Name" = Robbins;
Children = (
"Anthony's son 1 ",
"Anthony's daughter 1 ",
"Anthony's son 2 ",
"Anthony's son 3 ",
"Anthony's daughter 2"
);