iOS network development--json data decoding (using nsjsonserialization)

Source: Internet
Author: User
Let's take a case mynotes to learn how to use nsjsonserialization. Here, the data structure is redesigned in JSON format,

The contents of the memo information Notes.json document are as follows:

{"ResultCode": 0, "record": [
{"ID": "1", "CDate": "2012-12-23", "Content": "Publish IOSBook0", "UserID": "Tony"},
{" ID ":" 2 "," CDate ":" 2012-12-24 "," Content ":" Release IOSBook1 "," UserID ":" Tony "},
{" ID ":" 3 "," CDate ":" 2012-12-25 "," Content ":" Publish IOSBook2 "," UserID ":" Tony "},
{" ID ":" 4 "," CDate ":" 2012-12-26 "," Content ":" Release IOSBook3 "," UserID ":" Tony "},
{" ID ":" 5 "," CDate ":" 2012-12-27 "," Content ":" Publish IOSBook4 "," UserID ":" Tony "}]}
In fact, nsjsonserialization is easier to use, as long as you can make sure that your project uses the iOS 5 SDK. Modify View
Controller Masterviewcontroller The Viewdidload method, the specific code is as follows:

-(void) viewdidload {[Super viewdidload];
	
	Self.navigationItem.leftBarButtonItem = Self.editbuttonitem; Self.detailviewcontroller = (Detailviewcontroller *) [[Self.splitViewController.viewControllers Lastobject]
	Topviewcontroller]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reloadview:) name:@ "Relo
	
	Adviewnotification "Object:nil];
	path, the actual development to request server-side nsstring* path = [[NSBundle mainbundle] pathforresource:@ "Notes" oftype:@ "json"];
	NSData *jsondata = [[NSData alloc] initwithcontentsoffile:path];
	Nserror *error; The options parameter specifies the pattern of the following JSON://nsjsonreadingmutablecontainers-Specifies that the resolution returns a mutable array or dictionary. (This constant is the right choice)//nsjsonreadingmutableleaves.
	Specifies that the leaf node is a mutable string. Nsjsonreadingallowfragments. Specifies that the top-level node can be either an array or a dictionary id jsonobj = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers
	
	error:&error];
	if (!jsonobj | | error) {NSLOG (@ "JSON decoding failed");
} self.listdata = [jsonobj objectforkey:@ "Record"]; } 
In addition, Nsjsonserialization also provides JSON-encoded methods: DataWithJSONObject:options:error: And
WriteJSONObject:toStream:options:error:.


Related Article

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.