Parsing JSON data in iOS

Source: Internet
Author: User

Just in the afternoon that will get a small example of parsing XML demo, this thought of JSON is also quite complicated to get or not, but the simple look at the discovery is very simple

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 a list of data

@property (Nonatomic,strong) nsmutablearray * LISTDATA;

@end

The corresponding CHONVIEWCONTROLLER.M file code is as follows:

[OBJC] View plaincopy on code to see a snippet derived from my Code slice

//

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

Parsing JSON data in iOS

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.