JSON parsing for iOS and simple playback of videos

Source: Internet
Author: User

JSON parsing

1 JSON Brief introduction

1) What is JSON

(1) JSON is a lightweight data format that is typically used for data interaction

(2) data returned by the server to the client, usually in JSON format or XML format (except for file downloads)

2) Related instructions

(1) The format of JSON is much like the dictionary and array in OC

(2) Standard JSON format key must be double-quoted

3) JSON parsing scheme

A. Third-party framework Jsonkit\sbjson\touchjson

B. Apple Native (nsjsonserialization) (native most commonly used, also recommended)

2 JSON parsing related code

1) JSON data->oc object (//Convert JSON data to OC Object

-(void) JSONTOOC

{

1. Determine URL path

Nsurl *url = [Nsurl urlwithstring:@ "Http://120.25.226.186:32812/login?username=33&pwd=33&type=JSON"];

2. Create a Request object

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

3. Sending an asynchronous request using Nsurlsession

[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, Nserror * _nullable connectionerror) {

4. After receiving the data from the server response, parse the data (JSON--->oc)

/*

The first parameter: The JSON data to parse, which is the NSData type, which is the binary data

Second parameter: Optional configuration parameters for parsing JSON

Nsjsonreadingmutablecontainers parsed dictionaries and arrays are mutable

Nsjsonreadingmutableleaves parse out the string in the object is mutable iOS7 later there is a problem

Nsjsonreadingallowfragments parsed JSON data if it is neither a dictionary nor an array, you must use this

*/

Nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:kniloptions Error:nil];

NSLog (@ "%@", dict);

}];

}

2) OC Object->json Object (//1. OC object to convert to JSON data * Here is a dictionary

Nsdictionary *DICTM = @{

@ "name": @ "wendingding",

@ "Age": @100,

@ "Height": @1.73

};

2.oc->json

/*

Note: You can pass the + (BOOL) Isvalidjsonobject: (id) obj; method to determine whether the current OC object can be converted to JSON data

Specific restrictions:

1.obj is Nsarray or Nsdictionay, and they derive from subclasses

2.obj contains all objects that are nsstring,nsnumber,nsarray,nsdictionary or nsnull

3. All keys in the dictionary must be of type NSString

4.NSNumber objects cannot be Nan or infinity

*/

/*

The first parameter: The OC object to be converted to JSON data, here is a dictionary

Second parameter: nsjsonwritingprettyprinted the converted JSON object to be typeset, meaningless

*/

NSData *data = [nsjsonserialization datawithjsonobject:dictm options:nsjsonwritingprettyprinted Error:nil];

3. Print to see if data has a value

/*

First parameter: binary data to be converted to string

The second parameter: encoding, usually using a nsutf8stringencoding

*/

NSString *STRM = [[NSString alloc]initwithdata:data encoding:nsutf8stringencoding];

NSLog (@ "%@", StrM);

3) One by one correspondence between OC object and JSON data format

One by one correspondence between OC objects and JSON data

-(void) Ocwithjson

{

Various data formats for JSON

NSString *test = @ "\" wendingding\ "";

NSString *test = @ "true";

NSString *test = @ "{\" name\ ": \" wendingding\ "}";

->OC JSON data to objects to see the one by one correspondence between them

Note: How to parse JSON data if it is not a dictionary or an array (such as NSString), then you must use this nsjsonreadingallowfragments

ID obj = [nsjsonserialization jsonobjectwithdata:[test datausingencoding:nsutf8stringencoding] Options: Nsjsonreadingallowfragments Error:nil];

NSLog (@ "%@", [obj class]);

/* One by one correspondence between JSON data format and OC object

{} dictionary

Array of []

"", String

10/10.1-NSNumber

True/false-NSNumber

Null-NSNull

*/

}

}

4) How to view complex JSON data

Method One:

Online format Http://tool.oschina.net/codeformat/json

Method Two:

The parsed data is written in plist file, and the JSON hierarchy can be viewed intuitively through the plist file.

[DICTM writetofile:@ "/users/onlychenj/desktop/videos.plist" atomically:yes];

5//0. Need to import system framework

#import <MediaPlayer/MediaPlayer.h>

1. Get the data dictionary corresponding to the cell

Cjvideo *video = Self.videos[indexpath.row];

NSString *videostr = [@ "http://120.25.226.186:32812" stringByAppendingPathComponent:video.url];

2. Create a video player

Mpmovieplayerviewcontroller *VC = [[Mpmovieplayerviewcontroller alloc]initwithcontenturl:[nsurl URLWithString: VIDEOSTR]];

3.present Playback Controller

[Self PRESENTVIEWCONTROLLER:VC animated:yes completion:nil];

3 Dictionary Turn model frame

1) Related framework

A.mantle need to inherit from Mtmodel

B.jsonmodel need to inherit from Jsonmodel

C.mjextension does not require inheritance, no code intrusion

2) Issues to be aware of when designing and selecting a frame yourself

A. Invasive

B. Ease of use, easy to get started

C. Extensibility, it's easy to add new functionality to this framework

3) Simple use of the mjextension framework

1. Converting a dictionary array to a model array

Dictionary-to-model using the Mjextension framework

Self.videos = [Xmgvideo Mj_objectarraywithkeyvaluesarray:videoarray];

2. Renaming the name of a model property

The first method of renaming a property name, with some code intrusion

The ID in the settings dictionary is replaced by the ID in the model

+ (Nsdictionary *) mj_replacedkeyfrompropertyname

{

Return @{

@ "id": @ "id"

};

}

The second way to rename a property name is zero code intrusion

[Xmgvideo mj_setupreplacedkeyfrompropertyname:^nsdictionary *{

Return @{

@ "id": @ "id"

};

}];

3.MJExtension Framework Internal implementation principle-runtime

JSON parsing for iOS and simple playback of videos

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.