Serialization of array and dictionaries into JSON objects in iOS development

Source: Internet
Author: User

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"

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.