iOS Development Network Chapter-json Introduction

Source: Internet
Author: User

  What is Jsonjson is a lightweight data format, generally used for data interaction server returned to the client's data, usually in JSON format or XML format (except for file download)  json format is much like the dictionary in OC and the array {"Name"  :  "Jack",  "age"  : 10}{"names"  : ["Jack",  "Rose",  "Jim"]} Note points in the standard JSON format: key must be double-quoted   The JSON should be parsed for specific data to be mined from the JSON. That is, convert json  to  OC data type   Second, json – oc  conversion comparison Chart control relationship   III. JSON parsing scenario 1. In iOS, there are 4 common parsing scenarios for JSON (1) third-party frameworks: Jsonkit, Sbjson, Touchjson (performance from left to right, worse)   (2) Apple native (comes with): Nsjsonserialization (Best performance)  2.nsjsonserialization common methods (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. Parse JSON four, part of the code sample from the server #import  " YYViewController.h "#import   mbprogresshud+mj.h" @interface  YYViewController  () @property   ( weak, nonatomic)  iboutlet uitextfield *username; @property   (weak, nonatomic)  IBOutlet UITextField *pwd;-  ( ibaction) login; @end @implementation yyviewcontroller-  (ibaction) login {//     1. Advanced form Validation     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;    }//    2. Sending a request to the server (with an account number and password)      //Add a mask that prohibits user action     [mbprogresshud showmessage:@ "trying to load ..."];////     1. Setting the request path//    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];        // 1. Setting the request path      nsurl *url=[nsurl urlwithstring:@ "Http://192.168.1.53:8080/MJServer/login"];//does not need to pass parameters     //    2. Creating a Request Object     nsmutableurlrequest * request=[nsmutableurlrequest requestwithurl:url];//default to GET request      request.timeoutinterval=5.0;//Set request timeout to 5 seconds     [email protected] "POST";//Set Request method          //setting the request body     NSString *param=[NSString  stringwithformat:@ "username=%@&pwd=%@", self.username.text,self.pwd.text];    // Convert the stitched string to data and set the request body     request. httpbody=[param datausingencoding:nsutf8stringencoding];        Client type, can only write in English     [request setvalue:@ "ios+android"  forhttpheaderfield:@ " User-agent "];    //    3. Send request     //get a home row      nsoperationqueue *queue=[nsoperationqueue mainqueue];    [ nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse  *response, nsdata *data, nserror *connectionerror)  {         //called when the request ends (there are two results, one is to successfully get the data, or it may not have the data, the request failed)          [MBProgressHUD hideHUD];        if  (data)  {//request succeeded             nsdictionary *dict=[ nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves error:nil];             //After judging the login information in the interface prompt              nsstring *error=dict[@ "Error"];             if  (Error)  {                 [MBProgressHUD showError:error];             }else            {                 nsstring *success= dict[@ "Success"];                 [MBProgressHUD showSuccess:success];             }        }else   //request failed          {            [mbprogresshud showerror:@ "The network is busy, please try again later! "];        }    }];    nslog (@ "Request completed");} @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.