1 Preface
The datawithjsonobject: Options: Error: Method of the nsjsonserialization class is used for implementation. array and dictionary are serialized into JSON objects. It facilitates transmission over the network.
2. code example
Testdemo. m
[Plain]
(Void) conversetojson {
Nsmutabledictionary * dictionary = [[nsmutabledictionary alloc] init];
[Dictionary setvalue: @ "Archy" forkey: @ "first name"];
[Dictionary setvalue: @ "Robbins" forkey: @ "last name"];
[Dictionary setvalue: [nsnumber numberwithunsignedinteger: 51] forkey: @ "Age"];
Nsarray * arrayofarchyschildren = [[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: arrayofarchyschildren forkey: @ "children"];
Nserror * error = nil;
// Nsjsonwritingprettyprinted: specifies that spaces are used for the generated JSON data to make the output more readable. If this option is not set, the most compact JSON representation may be generated.
Nsdata * jsondata = [nsjsonserialization
Datawithjsonobject: dictionary options: nsjsonwritingprettyprinted error: & error];
If ([jsondata length]> 0 & error = nil ){
Nslog (@ "successfully serialized the dictionary into data .");
// Convert nsdata to string
Nsstring * jsonstring = [[nsstring alloc] initwithdata: jsondata encoding: nsutf8stringencoding];
Nslog (@ "JSON string = % @", jsonstring );
}
Else if ([jsondata length] = 0 &&
Error = nil ){
Nslog (@ "no data was returned after serialization .");
}
Else if (error! = Nil ){
Nslog (@ "an error happened = % @", error );
}
}
-(Void) conversetojson {
Nsmutabledictionary * dictionary = [[nsmutabledictionary alloc] init];
[Dictionary setvalue: @ "Archy" forkey: @ "first name"];
[Dictionary setvalue: @ "Robbins" forkey: @ "last name"];
[Dictionary setvalue: [nsnumber numberwithunsignedinteger: 51] forkey: @ "Age"];
Nsarray * arrayofarchyschildren = [[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: arrayofarchyschildren forkey: @ "children"];
Nserror * error = nil;
// Nsjsonwritingprettyprinted: specifies that spaces are used for the generated JSON data to make the output more readable. If this option is not set, the most compact JSON representation may be generated.
Nsdata * jsondata = [nsjsonserialization
Datawithjsonobject: dictionary options: nsjsonwritingprettyprinted error: & error];
If ([jsondata length]> 0 & error = nil ){
Nslog (@ "successfully serialized the dictionary into data .");
// Convert nsdata to string
Nsstring * jsonstring = [[nsstring alloc] initwithdata: jsondata encoding: nsutf8stringencoding];
Nslog (@ "JSON string = % @", jsonstring );
}
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
2013-05-13 17:14:26. 087 tojsontest [4890: 303] successfully serialized the dictionary into data.
17:14:26. 089 tojsontest [4890: 303] JSON string = {
"Children ":[
"Anthony's son 1 ",
"Anthony's daughter 1 ",
"Anthony's son 2 ",
"Anthony's son 3 ",
"Anthony's daughter 2"
],
"Age": 51,
"First name": "Archy ",
"Last Name": "Robbins"
}