JSON parsing implements interface data separation.

Source: Internet
Author: User
Tags uikit

Json

As a lightweight data interchange format, it is gradually replacing XML as a universal format for network data.

A subset of JavaScript-based

Easy to read, coding handwriting is difficult, data volume is small

The JSON format has replaced the XML for the network transmission is very convenient, but there is no clear XML, especially when the JSON data is very long, we will fall into the cumbersome and complex data node lookup

JSON format description

Object

{}

Structure of the key-value pairs for format {key:value, Key:value,...}

Can be deserialized into nsdictionary in OC

Array

[]

format ["Java", "JavaScript", "VB",...]

Can be deserialized into nsarray in OC

Tips

The JSON data format is very similar to the fast wrapping method in OC

The JSON data format also supports nesting

Parsing server-side return JSON data

Starting with iOS 5, use Nsjsonserialization to parse JSON

Other common three kinds of JSON parsing third-party libraries:

Sbjson because the API is easy to use, there may be some applications that remain

Jsonkit Jsonkit's developers say: Jsonkit's performance is better than Apple

Touchjson

Serialization and deserialization of JSON

Deserialization

[Nsjsonserialization jsonobjectwithdata:data options:0 error:null];

Serialization of

[Nsjsonserialization Datawithjsonobject:array options:0 error:null];

Nsjsonreadingoptions

Nsjsonreadingmutablecontainers = 1, the root node is variable

Nsjsonreadingmutableleaves = 2, node variable

Nsjsonreadingallowfragments = 4, the root node may not be nsdictionary or Nsarray

Code parsing steps

#import <Foundation/Foundation.h>

@interface Video:nsobject

@property (nonatomic, copy) NSNumber *videoid;

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSNumber *length;

@property (nonatomic, copy) NSString *videourl;

@property (nonatomic, copy) NSString *imageurl;

@property (nonatomic, copy) NSString *desc;

-(Instancetype) Initwithdict: (Nsdictionary *) dict;

+ (Instancetype) videowithdict: (Nsdictionary *) dict;

@end

#import "Video.h"

@implementation Video

-(Instancetype) Initwithdict: (Nsdictionary *) dict

{

self = [super init];

if (self) {

[Self setvaluesforkeyswithdictionary:dict];

}

return self;

}

+ (Instancetype) videowithdict: (Nsdictionary *) dict

{

return [[Self alloc] initwithdict:dict];

}

@end

Parsing steps

#import "PLMJSONViewController.h"

#import "Video.h"

@interface Plmjsonviewcontroller ()

@end

@implementation Plmjsonviewcontroller

/** overriding the load data method of the parent class */

-(void) LoadData

{

NSLog (@ "%s", __func__);

1. Url

Nsurl *url = [Nsurl urlwithstring:@ "Http://127.0.0.1/videos.json"];

2. Request

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

3. Sending an asynchronous request

[Nsurlconnection sendasynchronousrequest:request Queue:[[nsoperationqueue alloc] init] completionhandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {

Data is a JSON

The data is deserialized and parsed (many times we parse out a dictionary)

Nsarray *array = [nsjsonserialization jsonobjectwithdata:data options:0 error:null];

Create an array of videos

Nsmutablearray *arraym = [Nsmutablearray array];

For (nsdictionary *dict in array) {

[Arraym Addobject:[video videowithdict:dict];

}

Refresh data, update UI

Dispatch_async (Dispatch_get_main_queue (), ^{

Self.datalist = Arraym;

});

}];

}

@end

#import <UIKit/UIKit.h>

@interface Plmviewcontroller:uitableviewcontroller

@property (nonatomic, strong) Nsarray *datalist;

/** Loading Network data */

-(void) loaddata;

@end

#import "PLMViewController.h"

#import "Video.h"

@interface Plmviewcontroller ()

@end

@implementation Plmviewcontroller

/** set the array data for the table */

-(void) Setdatalist: (Nsarray *) dataList

{

_datalist = dataList;

Table of data source, bound in DataList, when the DataList content is reset, the table needs to be refreshed

[Self.tableview Reloaddata];

Stop refreshing a control

[Self.refreshcontrol endrefreshing];

}

-(void) viewdidload

{

[Super Viewdidload];

Load Network data

[Self loaddata];

}

-(Ibaction) Refresh

{

[Self loaddata];

}

-(void) LoadData

{

The specific implementation of this method is in the subclass

}

#pragma the mark-table data source method

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section

{

return self.dataList.count;

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

static NSString *id = @ "Cell";

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:id];

Video *v = Self.datalist[indexpath.row];

Cell.textLabel.text = V.name;

return cell;

}

@end

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.