IOS JSON parsing

Source: Internet
Author: User

Parse JSON into DIC object

  • -(void) Fetcheddata: (nsdata*) responsedata {//parse out the JSON datanserror* error;
  • nsdictionary* JSON =[nsjsonserialization
  • Jsonobjectwithdata:responsedata //1
  • Options:kniloptions
  • error:&error];
  • nsarray* Latestloans =[json objectforkey:@"loans"]; //2
  • NSLog (@"loans:%@", Latestloans); //3
  • }
  • To generate a JSON string for an object
  • Build a Info object and convert to JSON
  • nsdictionary* info =[nsdictionary dictionarywithobjectsandkeys:[loan objectforkey:@"name"],
  • @"who",
  • [(nsdictionary*) [Loan objectforkey:@"location"]
  • objectforkey:@"Country"],
  • @"where",
  • [NSNumber Numberwithfloat:outstandingamount],
  • @"What",
  • NIL];
  • Convert object to Data
  • nsdata* Jsondata =[nsjsonserialization Datawithjsonobject:info
  • Options:nsjsonwritingprettyprinted error:&error];
  • Print out the data contents
  • Jsonsummary.text =[[nsstring Alloc] Initwithdata:jsondata
  • Encoding:nsutf8stringencoding];
  • Add JSON method to dic
  • @interfaceNSDictionary (jsoncategories)
  • + (nsdictionary*) dictionarywithcontentsofjsonurlstring: (nsstring*) urladdress;
  • -(nsdata*) ToJSON;
  • @end
  • @implementationNSDictionary (jsoncategories)
  • + (nsdictionary*) dictionarywithcontentsofjsonurlstring: (nsstring*) urladdress{
  • nsdata* data =[nsdata Datawithcontentsofurl:[nsurl urlwithstring:urladdress];
  • __autoreleasing nserror* error =nil;
  • ID result =[nsjsonserialization Jsonobjectwithdata:data
  • Options:kniloptions error:&error];
  • if (Error!=nil) Returnnil;
  • return result;
  • }
  • -(nsdata*) tojson{
  • nserror* error =nil;
  • ID result =[nsjsonserialization datawithjsonobject:self
  • Options:kniloptions error:&error];
  • if (Error!=nil) Returnnil;
  • return result;
  • }@end
  • Use the
  • nsdictionary* myinfo =[nsdictionary dictionarywithcontentsofjsonurlstring:@"Http://www.yahoo.com/news.json"];
  • nsdictionary* information =[nsdictionary dictionarywithobjectsandkeys:@"Orange" @"Apple" @ "Banana" @  "Fig", nil];
  • nsdata* JSON =[information ToJSON];
  • Determine if JSON is possible
  • BOOL Isturnabletojson =[nsjsonserialization Isvalidjsonobject:object]

IOS JSON parsing

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.