Analysis of-json data in iOS Development network chapter

Source: Internet
Author: User

 analysis of-json data in iOS Development Network Chapter

iOS Development Network Chapter-json Introduction

First, what is JSON

JSON is a lightweight data format that is typically used for data interaction

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

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

{"Name": "Jack", "Age": 10}

{"Names": ["Jack", "Rose", "Jim"]}

Note points in the standard JSON format: key must be double-quoted

Second, JSON–OC conversion table

Control relationship

Three, JSON parsing scheme

1. In iOS, there are 4 common parsing scenarios for JSON

(1) Third-party framework: Jsonkit, Sbjson, Touchjson (performance from left to right, worse)

(2) Apple native (comes with): Nsjsonserialization (Best performance)

Common methods of 2.NSJSONSerialization

(1) JSON data-"OC Object"

+ (ID) jsonobjectwithdata: (NSData *) Data options: (nsjsonreadingoptions) opt error: (NSERROR *) error;

(2) OC Object-"JSON data"

+ (NSData *) Datawithjsonobject: (ID) obj options: (nsjsonwritingoptions) opt error: (NSERROR *) error;

3. Parsing JSON from the server

Iv. part of the code example

 1 #import "YYViewController.h" 2 #import "Mbprogresshud+mj.h" 3 4 @interface Yyviewcontroller () 5 @property (weak, Nona Tomic) Iboutlet Uitextfield *username; 6 @property (weak, nonatomic) Iboutlet Uitextfield *pwd; 7-(ibaction) login; 8 9 @end10 @implementation YYViewController12-(ibaction) Login {14//1. Advance form verification of the IF (self.username.text.le          ngth==0) {Mbprogresshud showerror:@ "Please enter user name"];17 return;18}19 if (self.pwd.text.length==0) {20      [Mbprogresshud showerror:@ "Please enter password"];21 return;22}23//2. Send a request to the server (with an account number and password) 24//Add a mask to prevent user action 25 [Mbprogresshud showmessage:@ "is trying to load in ...."];26 27//28//1. Set Request path//NSString *urlstr=[nsstring stringwithfor    mat:@ "", self.username.text,self.pwd.text];30// Nsurl *url=[nsurl urlwithstring:urlstr];31 32//1. Set Request path Nsurl *url=[nsurl urlwithstring:@ "http://192.168.1 .53:8080/mjserver/login "];//no need to pass parameter 34    35//2. Create Request object Nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl:url];//default to get request PNS . timeoutinterval=5.0;//set request timeout of 5 seconds [email protected] "POST";//Set Request Method 39 40//Set request body NSString *param=[n Sstring stringwithformat:@ "username=%@&pwd=%@", self.username.text,self.pwd.text];42//Convert the stitched string to data, Sets the request body.  Httpbody=[param datausingencoding:nsutf8stringencoding];44 45//client type, only written in English [request setvalue:@ "Ios+android" forhttpheaderfield:@ "User-agent"];47 48//3. Send request 49//Get a home row from the Nsoperationqueue *queue=[nsoperationqueue mainqueue];51 [nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse * Response, NSData *data, Nserror *connectionerror) {52//call at the end of the request (there are two results, one is to successfully get the data, or you may not have the data, the request failed) [MBPR Ogresshud hidehud];54 if (data) {//Request successfully nsdictionary *dict=[nsjsonserialization jsonobjectwithdata:d ATA Options:nsjsonreadinGmutableleaves error:nil];56//After judging the login information in the interface prompt nsstring *error=dict[@ "error"];58 if (E Rror) {Mbprogresshud showerror:error];60}else61 {nsstring *success=dict[@ "Success"];63 [Mbprogresshud showsuccess:success];64}65}else//request failed [Mbprogresshud showerror:@] Network busy, please try again later! "];68}69}];70 NSLog (@" request Sent ");}72 @end

iOS Development Network Chapter-json data parsing

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