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:.