Ios json data parsing

Source: Internet
Author: User

In the afternoon, I got a small example of parsing the xml demo. I thought the json is complicated and I still don't want to get it. But after a simple look, I found it quite simple and hesitated at this time, do you want to write it to your blog?

After a long time of consideration, I 'd like to write it down. After all, json is used too much, and it can be regarded as my own accumulation. After all, when I first started to learn about IOS development, I thought it was to consolidate it!

Let's take a look at it first, as shown below!



Next, let's take a look at the project directory. In fact, it's not necessary to directly create a project. If you forget it, paste it as follows:


There is a Notes. json file in the project directory, which is the json data to be parsed. Cut it down as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140517/2014051709100322.jpg" alt = "\">


OK, the above preparations are complete, and the encoding is started. Before that, the content of the Story version is configured in the same way as that of the IOS parsing xml Story version of my previous blog article, first, let's take a look at 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
 
  
@ Interface chonViewController: UITableViewController // Save the 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] initWithContentsOfF Ile: path]; NSError * error; id jsonObj = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: & error]; if (! JsonObj | error) {NSLog (@ "JSON parsing 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 progress: @ "Cell" forIndexPath: indexPath]; NSMutableDictionary * dict = self. listData [indexPath. row]; cell. textLabel. text = [dict objectForKey: @ "Content"]; cell. detailTextLabel. text = [dict objectForKey: @ "CDate"]; return cell;}-(void) didreceivemorywarning {[super didreceivemorywarning];} @ end

OK, now you can compile the running program, but there is an idea:

1. Because NSJSONSerialization is used for json decoding, make sure that you have to use the IOS 5 SDK for the project.

2. Nothing else. Let's introduce the class method of NSJSONSerialization.

1) NSJSONReadingMutableContaines: Specifies the variable array or dictionary returned by parsing. This method is still relatively used, because if json data needs to be changed, you do not need to scatter it.

2) NSJSONReadingMutableLeaves: specifies that the leaf node is a variable string.

3) NSJSONReadingAllowFragments: specifies that arrays or dictionaries can be deployed on top-level nodes.


In addition, json is a lightweight data exchange format .... What, go to your uncle, but it is preferred to consider json rather than xml during application development. xml should first consider TBXML. As mentioned in the previous blog, json is the first

Consider NSJSONSerialization.




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.