First, what is a simple introduction to JSON data 1.JSON
JSON: is a lightweight format for transmitting data for the interaction of data
JSON is a subset of the JavaScript language. JavaScript is a scripting language (no compilation required) to add dynamic functionality to HTML.
JavaScript and Java don't have a half-penny relationship!
The data returned to the client by the server is generally in JSON or XML format (except for file downloads).
Syntax rules for 2.JSON
<1> data are stored as key values;
The key (key) must be enclosed in double quotation marks ("key"), separated from the key value by ': '; {"Name": "Xiaoming"}
<2> data and data are separated by commas (,). {"Name": "Xiaoming", "Age": 13}
<3> {} represents the object. "Person": {"name": "Xiaoming", "Age": 13}
<4> [] represents an array. "Persons": [
{"Name": "Xiaoming", "Age": 13},
{"Name": "Little Red", "Age": 14},
{"Name": "Floret", "Age": 15}
]
3.JSON vs OC Conversion control
Curly braces {} = = Nsdictionary;
brackets [] = = Nsarray;
Double quotes "" = = NSString;
Digital 13,13.5 = = NSNumber;
Note : Numbers are recommended to be received using NSNumber, in order to better assign values to null
Second, JSON parsing (deserialization)
Deserialization: Converts the JSON data (binary data) received from the server to the OC data type (nsarray,nsdictionary, etc.) The process.
Objective: JSON data---OC object; Get a data dictionary or an array of data
Benefits: Simplify the development of the program, easy to follow the dictionary to model.
The way 1.JSON data is parsed
In iOS, there are 4 common JSON data parsing scenarios:
Third-party framework: Jsonkit, Sbjson, Touchjson. Performance from left to right, and in turn, poor. (Before IOS 5 (2011))
Apple native (comes with):nsjsonserialization (performance is the best. IOS5 later).
Routine Demo:
Read the JSON file from a locally built server and show it to a TableView
Local server resources:
JSON file
:
Code implementation:
gxvideo.h// 03-json Data parsing//// Created by Gxiangzi on 15/8/17.// Copyright (c) 2015 Hqu. All rights reserved.//#import <Foundation/Foundation.h> @interface gxvideo:nsobject@property (copy, nonatomic) nsstring* length; @property (copy, nonatomic) nsstring* URL, @property (copy, nonatomic) nsstring* image; @property (copy, n onatomic) nsstring* ID; @property (copy, nonatomic) nsstring* name;+ (instancetype) videowithdict: (Nsdictionary *) dict;@ End
gxvideo.m// 03-json Data parsing//// Created by Gxiangzi on 15/8/17.// Copyright (c) 2015 Hqu. All rights reserved.//#import "GXVideo.h" @implementation gxvideo+ (Instancetype) videowithdict: (Nsdictionary *) dict{ Gxvideo *video = [[Self alloc] init]; [Video setvaluesforkeyswithdictionary:dict]; return video;} @end
gxviewcontroller.h// 03-json Data parsing//// Created by Gxiangzi on 15/8/17.// Copyright (c) 2015 Hqu. All rights reserved.//#import <UIKit/UIKit.h> @interface gxviewcontroller:uitableviewcontroller@end
gxviewcontroller.m//03-json Data parsing////Created by Gxiangzi on 15/8/17.//Copyright (c) 2015 Hqu. All rights reserved.//#import "GXViewController.h" #import "GXVideo.h" #import "uiimageview+webcache.h" #import < Mediaplayer/mediaplayer.h> @interface Gxviewcontroller () @property (nonatomic, strong) nsmutablearray* videos;@ End@implementation gxviewcontroller#pragma mark-Lazy load-(nsmutablearray*) videos{if (!_videos) {_videos = [NSMuta Blearray array]; } return _videos;} -(void) viewdidload{[Super Viewdidload]; Parse data from the network nsstring* urlstring = @ "Http://localhost/resources/vedios.json"; nsurl* url = [Nsurl urlwithstring:urlstring]; Create a network request nsurlrequest* requests = [Nsurlrequest Requestwithurl:url]; __weak typeof (self) wself = self; Send a network request [Nsurlconnection sendasynchronousrequest:request Queue:[nsoperationqu Eue Mainqueue] completionhandler:^ (nsurlresponse* response, NSDAta* data, nserror* connectionerror) {///After successful link return data// Convert to JSON nsarray* arrayvideo = [Nsjsonserialization jsonobjectwithdata:data options:nsjsonrea Dingmutablecontainers Error:null]; [Arrayvideo enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, bool* stop) {nsdictionary* dict = obj; gxvideo* video = [Gxvideo videowithdict:dict]; [Wself.videos Addobject:video]; }]; [Wself.tableview Reloaddata]; }];} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} #pragma mark-table View data source-(Nsinteger) TableView: (uitableview*) TableView numberofrowsinsection: (Nsinteger) section{return self.videos.count;} - (uitableviewcell*) TableView: (uitableview*) TableView Cellforrowatindexpath: (nsindexpath*) indexpath{static NSString * Resuedid = @ "Cell"; uitableviewcell* cell = [TableView Dequeuereusablecellwithidentifier:resuedid]; if (!cell) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:resuedid]; } gxvideo* video = Self.videos[indexpath.row]; Cell.textLabel.text = Video.name; nsstring* timedetail = [NSString stringwithformat:@ "duration:%@ minutes", video.length]; Cell.detailTextLabel.text = Timedetail; Using the tripartite framework sdwebimage [Cell.imageview Sd_setimagewithurl:[nsurl URLWithString:video.image] Placeholderimage:[uiimage imagenamed:@ "Placeholder-1"]; return cell;} #pragma mark-Play video-(void) TableView: (uitableview*) TableView Didselectrowatindexpath: (nsindexpath*) indexpath{ gxvideo* video = Self.videos[indexpath.row]; nsurl* url = [Nsurl URLWithString:video.url]; Create a player mpmovieplayerviewcontroller* media = [[MpmoviepLayerviewcontroller alloc] Initwithcontenturl:url]; [Self presentmovieplayerviewcontrolleranimated:media];} @end
iOS Development Network Chapter-json file parsing