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"
);