Convert a dictionary or array into JSON data or a string

Source: Internet
Author: User

Convert a dictionary or array into JSON data or a string

Source:

Nsdictionary+json.h and nsdictionary+json.m

////Nsdictionary+json.h//Category////Created by youxianming on 15-2-09.//Copyright (c) 2014 youxianming. All rights reserved.//#import<Foundation/Foundation.h>@interfacensdictionary (JSON)/** * Convert to JSON string (no readability) * * @return JSON string*/-(NSString *) tojsonstring;/** * Convert to JSON string (readable) * * @return JSON string*/-(NSString *) toreadablejsonstring;/** * Convert to JSON data * * @return JSON data*/-(NSData *) Tojsondata;@end
////nsdictionary+json.m//Category////Created by youxianming on 15-2-09.//Copyright (c) 2014 youxianming. All rights reserved.//#import "Nsdictionary+json.h"@implementationnsdictionary (JSON)-(NSString *) tojsonstring {nsdata*data =[Nsjsonserialization datawithjsonobject:self OPTIONS:NSJSONREADINGM Utableleaves|nsjsonreadingallowfragments Error:nil]; if(Data = =Nil) {        returnNil; } nsstring*string=[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; return string;}-(NSString *) toreadablejsonstring {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

Nsarray+json.h and nsarray+json.m

////Nsarray+json.h//Category////Created by youxianming on 15-2-09.//Copyright (c) 2014 youxianming. All rights reserved.//#import<Foundation/Foundation.h>@interfaceNsarray (JSON)/** * Convert to JSON string (no readability) * * @return JSON string*/-(NSString *) tojsonstring;/** * Convert to JSON string (readable) * * @return JSON string*/-(NSString *) toreadablejsonstring;/** * Convert to JSON data * * @return JSON data*/-(NSData *) Tojsondata;@end
////nsarray+json.m//Category////Created by youxianming on 15-2-09.//Copyright (c) 2014 youxianming. All rights reserved.//#import "Nsarray+json.h"@implementationNsarray (JSON)-(NSString *) tojsonstring {nsdata*data =[Nsjsonserialization datawithjsonobject:self OPTIONS:NSJSONREADINGM Utableleaves|nsjsonreadingallowfragments Error:nil]; if(Data = =Nil) {        returnNil; } nsstring*string=[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; return string;}-(NSString *) toreadablejsonstring {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

Use:

Convert a dictionary or array into JSON data or a string

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.