Just in the afternoon that will get a small example of parsing XML demo, the thought of JSON is also quite complicated or not to get, but simple to see the next found quite simple, this time hesitate, or write to blog?
Think for a long time, or write it up, after all, JSON used too much, and it is their own accumulation, after all, just started learning iOS development knowledge, it is to consolidate the Satan!
Let's take a look at it first, as follows!
Next look at the project catalog bar, in fact, there is no need, directly set up a project on the line, forget it, or paste it up, as follows:
The project directory has a Notes.json file, which is the JSON data to parse, also cut it, as follows:
Ok, the above is ready to begin coding, before this story version of the content and my previous blog post IOS parsing XML story version is the same configuration, here is not verbose, first look at the ChonViewController.h file, the code is as follows:
chonviewcontroller.h// testjson//// Created by Choni on 14-5-16.// Copyright (c) 2014 Choni. All rights reserved.//#import <UIKit/UIKit.h> @interface chonviewcontroller:uitableviewcontroller// Save Data List @property (nonatomic,strong) nsmutablearray * LISTDATA; @end
The corresponding CHONVIEWCONTROLLER.M file code is as follows:
chonviewcontroller.m//testjson////Created by Choni on 14-5-16.//Copyright (c) 2014 Choni. All rights reserved.//#import "ChonViewController.h" @interface Chonviewcontroller () @end @implementation chonviewcontroller-(void) viewdidload{[Super Viewdidload]; NSString * Path = [[NSBundle mainbundle]pathforresource:@ "Notes" oftype:@ "json"]; NSData * Jsondata = [[NSData alloc] initwithcontentsoffile:path]; Nserror * ERROR; ID jsonobj = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:& ERROR]; if (!jsonobj | | error) {NSLOG (@ "JSON parse failed"); } self.listdata = [jsonobj objectforkey:@ "Record"]; } #pragma mark-tableview-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return Self.listData.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "Cell" ForIndexPath: Indexpath]; Nsmutabledictionary * dict = Self.listdata[indexpath.row]; Cell.textLabel.text = [Dict objectforkey:@ "Content"]; Cell.detailTextLabel.text = [Dict objectforkey:@ "CDate"]; return cell;} -(void) didreceivememorywarning{[super didreceivememorywarning];} @end
Ok, now it's time to compile the running program, but here's an idea:
1. Because JSON decoding is implemented using Nsjsonserialization, make sure your project uses the iOS 5 SDK to
2. There is nothing else, so let's introduce the class method of Nsjsonserialization.
1) Nsjsonreadingmutablecontaines, specifying that parsing returns a mutable array or dictionary, this method is still relatively used, because if the JSON data needs to be changed, do not control the
2) Nsjsonreadingmutableleaves, specifies that the leaf node is a variable string
3) Nsjsonreadingallowfragments, specifies that the top-level node can deploy an array or dictionary
In addition, JSON is a lightweight data interchange format .... What, go to your uncle's, but in the application development of the preferred to consider JSON rather than XML, XML is the first consideration should be tbxml, the previous blog mentioned, JSON first
Consider Nsjsonserialization.