Recently used the Jison analysis, said that the beginning of the time really do not understand, chaos, get a bit of a pass, and slowly have a general understanding. Here is an overview of my simple use of network requests and Jison parsing.
1. Version issues.
Due to the evolution of the iOS version, the system above the IOS9 has different requirements for network transmission, a new attribute app Transport Security Settings is added above 9.0, which requires that we must be the HTTPS protocol in the network request. Otherwise, the following error will occur:
The resource could not being loaded because the APP Transport Security policy requires the use of a secure connection.
But for our personal developers, HTTPS is not so easy, the general will still use the previous HTTP protocol, then we need to configure something
(1) Add a line "App Transport Security Settings" in the Info.plist file (and the location of the same folder in Main.storyboard), type nsdictionary
(2) Add a bool type attribute "allow arbitrary Loads" to the new line and set it to Yes.
2. Network request and parse the resulting data
Because I only used a very simple synchronous get method, so this is the only way to write this method (others later mastered the re-written)
//to build a string for a network requestNSString * urlstring=@"*******"; //If there is Chinese in the URL, do urlencodeNSString *newurlstr =[URLString stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; //build the Network URL object, NsurlNsurl *url =[Nsurl Urlwithstring:newurlstr]; //Create a network requestNsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata timeOutInterval:Ten]; //Create a sync linkNsurlresponse *response =Nil; Nserror*error =Nil; NSData*data = [Nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&ERROR]; if(data.length>0) { //Convert the resulting data to the appropriate object (determined after viewing the Jison format)Nsdictionary *result=[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; }
The above is the process of requesting and getting the data and then parsing it out.
Because Chinese encoding is often problematic, we specify its encoding in a steady manner.
Jison format generally has a dictionary, the form of an array, you can directly open the connection to view the Jison format, and then determine its conversion why.
3. Additional content
In this Jison analysis, I used some self-thought helpful knowledge, remember, later to see.
Nsdictionary Fast value: dictionary[@ "key Name"];
NSNumber comparison:
NSNumber *state=dictionary[@ " quantity "]; if ([State Isequaltonumber:[nsnumber numberwithint:]]==yes) // quantity equals ten {}
oc-Network request (get) and Jison parsing for iOS development