iOS Development Network Chapter-json Introduction

Source: Internet
Author: User

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

To dig out specific data from JSON, you have to parse the JSON. That is, convert JSON to OC data type

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 @interfaceYyviewcontroller ()5@property (Weak, nonatomic) Iboutlet Uitextfield *username;6@property (Weak, nonatomic) Iboutlet Uitextfield *pwd;7-(ibaction) login;8 9 @endTen  One @implementationYyviewcontroller A  --(ibaction) Login { - //1. Advanced Form Validation the     if(self.username.text.length==0) { -[Mbprogresshud ShowError:@"Please enter user name"]; -         return; -     } +     if(self.pwd.text.length==0) { -[Mbprogresshud ShowError:@"Please enter your password"]; +         return; A     } at //2. Send the request to the server (with the account number and password) -     //Add a mask to prevent user action -[Mbprogresshud ShowMessage:@"trying to load in ...."]; -  - // - //1. Set the request path in //nsstring *urlstr=[nsstring stringwithformat:@ "http://192.168.1.53: 8080/mjserver/login?username=%@&pwd=%@ ", Self.username.text,self.pwd.text]; - //nsurl *url=[nsurl urlwithstring:urlstr]; to      +     //1. Set the request path -Nsurl *url=[nsurl urlwithstring:@"Http://192.168.1.53:8080/MJServer/login"];//no need to pass parameters the      * //2. Create a Request object $Nsmutableurlrequest *request=[nsmutableurlrequest Requestwithurl:url];//default is GET requestPanax NotoginsengRequest.timeoutinterval=5.0;//Set request timeout to 5 seconds -Request. Httpmethod=@"POST";//Set Request Method the      +     //set the request body ANSString *param=[nsstring stringWithFormat:@"username=%@&pwd=%@", Self.username.text,self.pwd.text]; the     //convert the stitched string to data, set the request body +Request. httpbody=[param datausingencoding:nsutf8stringencoding]; -      $     //client type, can only write in English $[Request SetValue:@"ios+android"Forhttpheaderfield:@"user-agent"]; -      - //3. Sending the request the     //get a home row -Nsoperationqueue *queue=[Nsoperationqueue Mainqueue];Wuyi[Nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse *response, NSData * Data, Nserror *connectionerror) { the         //called at the end of the request (there are two results, one is to get the data successfully, or not to get the data, the request failed) - [Mbprogresshud Hidehud]; Wu         if(data) {//Request succeedednsdictionary *dict=[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Err Or:nil]; About             //After judging the login information in the interface prompt $           nsstring *error=dict[@ "error"]; -             if(Error) { - [Mbprogresshud Showerror:error]; -}Else A             { +NSString *success=dict[@"Success"]; the [Mbprogresshud showsuccess:success]; -             } $}Else   //request failed the         { the[Mbprogresshud ShowError:@"the network is busy, please try again later! "]; the         } the     }]; -NSLog (@"Send request Complete"); in } the @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.