IOS development (75) converts JSON data into Arrays or Dictionaries

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.