Conversions between dictionaries, arrays, and JSON small demo

Source: Internet
Author: User

/** * Array to JSON.*/- (void) testjsonandarray{Nsarray*arr = [Nsarray arraywithobjects:@"a",@"b",@"C",@"D",@"e",@"F", nil]; NSLog (@"Arrar =%@", arr); Nserror*error =Nil; NSData*jsondata = [nsjsonserialization Datawithjsonobject:arr options:0error:&ERROR]; if(!error) {NSString*json =[[NSString alloc] Initwithdata:jsondata encoding:nsutf8stringencoding]; NSLog (@"json =%@", JSON); }}
/** * Test JSON and object, dictionary to JSON*/- (void) testjsonanddict{nsdictionary*dict = [Nsdictionary Dictionarywithobjectsandkeys:@"Zhang San",@"name",                          @" -",@" Age",@" the",@"Height", nil]; NSLog (@"DIC =%@", Dict); Nserror*error =Nil; NSData*data = [Nsjsonserialization datawithjsonobject:dict options:nsjsonwritingprettyprinted error:&ERROR]; NSString*json =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; if(Error = =Nil) {NSLog (@"dictionary to json successful, JSON =%@", JSON); }    }

/** * Convert json to object, possibly a dictionary, possibly an array. * * @param JSON string passed in*/- (void) Jsontoobjectwithjosn: (NSString *) json{//a data format used by JSON to transmit data over a network. is a special kind of string. NSData*jsondata =[JSON datausingencoding:nsutf8stringencoding]; Nserror*error =Nil; IDresult = [Nsjsonserialization jsonobjectwithdata:jsondata options:kniloptions error:&ERROR]; if(Error = =Nil) {NSLog (@"%@ -- %@", Result,[resultclass]); }    }

Conversions between dictionaries, arrays, and JSON small demo

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.