Nsjsonserialization can handle the Jsondata
The nsjsonserialization class to convert JSON to Foundation objects and convert Foundation objects to json.
You use the Nsjsonserialization class to convert the JSON data into a foundation object or convert the foundation object into JSON data.
An object, the May is converted to JSON must has the following properties:
An object that can be converted to JSON data must have the following characteristics:
- The top level object was an Nsarray or nsdictionary.
- All objects is instances of nsstring, nsnumber, Nsarray, nsdictionary, or NSNull.
- All dictionary keys is instances of nsstring.
- Numbers is not NaN or infinity.
- The topmost object must be Nsarray or nsdictionary
- All instance objects must be NSString, NSNumber, Nsarray, nsdictionary, or Nsnull
- The key values in all dictionaries must be nsstring
- Numbers must be meaningful (cannot be infinite)
Provide the source code for the test use:
Nsdictionary+json.h and NSDICTIONARY+JSON.M
////Nsdictionary+json.h//Category////Created by youxianming on 14-8-28.//Copyright (c) 2014 youxianming. All rights reserved.//#import<Foundation/Foundation.h>@interfacensdictionary (JSON)//convert into jsonstring-(NSString *) tojsonstring;//convert into Jsondata-(NSData *) Tojsondata;@end
////nsdictionary+json.m//Category////Created by youxianming on 14-8-28.//Copyright (c) 2014 youxianming. All rights reserved.//#import "Nsdictionary+json.h"@implementationnsdictionary (JSON)-(NSString *) tojsonstring{NSData*data =[Nsjsonserialization datawithjsonobject:self OPTIONS:NSJSONWRITINGP Rettyprinted Error:nil]; if(Data = =Nil) { returnNil; } nsstring*string=[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; return string;}-(NSData *) tojsondata{NSData*data =[Nsjsonserialization datawithjsonobject:self OPTIONS:NSJSONWRITINGP Rettyprinted Error:nil]; returndata;}@end
Nsdata+json.h and NSDATA+JSON.M
// // nsdata+json.h// Category//// Created by Youxianming on 14-8-28. // Copyright (c) 2014 youxianming. All rights reserved. // #import <Foundation/Foundation.h>@interface nsdata (JSON)// Convert to set -(ID) topropertylist; @end
////nsdata+json.m//Category////Created by youxianming on 14-8-28.//Copyright (c) 2014 youxianming. All rights reserved.//#import "Nsdata+json.h"@implementationNSData (JSON)- (ID) topropertylist{return[Nsjsonserialization jsonobjectwithdata:self Options:nsjsonreadingmutablele Aves Error:nil];}@end
When using the source code:
////VIEWCONTROLLER.M//JSON////Created by youxianming on 14-10-8.//Copyright (c) 2014 youxianming. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Organizing JSON dataNsdictionary *jsondic = @{ @"name":@"youxianming",//NSString @" Age": @ -,//NSNumber @"BWH": @[@1, @1, @1],//Nsarray @"Address": @{@"Beijin":@"XXXX",@"xianning":@"YYYY"},//nsdictionary @"Hasgirlfriend": [NSNullNULL]//NSNull }; //convert into JsondataNSData *jsondata =[Jsondic Tojsondata]; //convert Jsondata to listNSLog (@"%@", [Jsondata topropertylist]);}@end
Here are the main points:
There is one more thing to note:
All number-related types are converted to NSNumber, either Boolean or floating-point values or integer values, and are converted to NSNumber.
Nsjsonserialization can handle the Jsondata