Iosjson (convert a dictionary or array into a JSON string)

Source: Internet
Author: User
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

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.