1. Convert NSDictionary or NSArray into a JSON string and convert the dictionary or array into a JSON string
1. Convert NSDictionary or NSArray to a JSON string // convert the dictionary or array to a JSON string-(NSData *) toJSONData :( id) theData {NSError * error = nil; NSData * jsonData = [NSJSONSerialization dataWithJSONObject: theData options: NSJSONWritingPrettyPrinted error: if
1. Convert NSDictionary or NSArray into a JSON string
// Convert a dictionary or array into a JSON string
-(NSData *) toJSONData :( id) theData {
NSError * error = nil;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject: theData
Options: NSJSONWritingPrettyPrinted
Error: & error];
If ([jsonData length]> 0 & error = nil ){
Return jsonData;
} Else {
Return nil;
}
}
With this method, we can get the desired JSON string
NSString * jsonString = [[NSString alloc] initWithData: jsonData
Encoding: NSUTF8StringEncoding];
2. Convert the JSON string to NSDictionary or NSArray
Convert NSString to NSData
[JsonString dataUsingEncoding: NSASCIIStringEncoding];
// Convert a JSON string into a dictionary or Array
-(Id) toArrayOrNSDictionary :( NSData *) jsonData {
NSError * error = nil;
Id jsonObject = [NSJSONSerialization JSONObjectWithData: jsonData
Options: NSJSONReadingAllowFragments
Error: & error];
If (jsonObject! = Nil & error = nil ){
Return jsonObject;
} Else {
// Resolution Error
Return nil;
}
}
3. encapsulate JSON strings with NSArray and NSDictionary operations
Of course, there are also many times when we define these operations in a classification of NSObject and NSString respectively.
Direct paste:
1. Convert NSString to NSArray or NSDictionary
# Import "NSString + JSONCategories. h"
@ Implementation NSString (JSONCategories)
-(Id) JSONValue;
{
NSData * data = [self dataUsingEncoding: NSUTF8StringEncoding];
_ Autoreleasing NSError * error = nil;
Id result = [NSJSONSerialization JSONObjectWithData: data options: kNilOptions error: & error];
If (error! = Nil) return nil;
Return result;
}
@ End
2. Convert NSArray or NSDictionary to NSString
# Import "NSObject + JSONCategories. h"
@ Implementation NSObject (JSONCategories)
-(NSData *) JSONString;
{
NSError * error = nil;
Id result = [NSJSONSerialization dataWithJSONObject: self
Options: kNilOptions error: & error];
If (error! = Nil) return nil;
Return result;
}
@ End
<无>
1. Convert NSDictionary or NSArray to a JSON string // convert the dictionary or array to a JSON string-(NSData *) toJSONData :( id) theData {NSError * error = nil; NSData * jsonData = [NSJSONSerialization dataWithJSONObject: theData options: NSJSONWritingPrettyPrinted error: & error]; if ([jsonData length]> 0 & error = nil) {return jsonData ;} else {return nil;} returns the result using this method. We can get the desired JSON string NSString * jsonString = [[NSString alloc] initWithData: jsonData encodin G: NSUTF8StringEncoding]; 2. convert a JSON string to NSDictionary or NSArray to NSData [jsonString dataUsingEncoding: NSASCIIStringEncoding]; // convert the JSON string to a dictionary or array-(id) toArrayOrNSDictionary :( NSData *) jsonData {NSError * error = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData: jsonData options: response error: & error]; if (jsonObject! = Nil & error = nil) {return jsonObject;} else {// resolution error return nil ;}} 3. encapsulate the JSON string and NSArray and NSDictionary operations, there are also many times when we define these operations in a classification of NSObject and NSString respectively: 1. convert NSString to NSArray or NSDictionary # import "NSString + JSONCategories. h "@ implementation NSString (JSONCategories)-(id) JSONValue; {NSData * data = [self dataUsingEncoding: Custom]; _ autoreleasing NSError * error = nil; id result = [NSJ SONSerialization JSONObjectWithData: data options: kNilOptions error: & error]; if (error! = Nil) return nil; return result;} @ end2. convert NSArray or NSDictionary to NSString # import "NSObject + JSONCategories. h "@ implementation NSObject (JSONCategories)-(NSData *) JSONString; {NSError * error = nil; id result = [NSJSONSerialization dataWithJSONObject: self options: kNilOptions error: & error]; if (error! = Nil) return nil; return result;} @ end