Converting JSON data into arrays or dictionaries in iOS development

Source: Internet
Author: User
Tags convert json to array

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"

);

 

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.