In the development of iOS network, it is generally related to the network request interface, which is inseparable from the processing of JSON data. JSON data is used as a format for each front-end and background interaction, with the advantages of cross-platform, lightweight data volume, and simple and understandable.
JSON is widely used in scenarios where ANDROID,IOS,HTML5 equals background server interaction, and JSON is a cross-platform interactive data format, with the conversion of objects to binary data, because the data types that pass information recognition between different platforms are binary.
The core of JSON data processing in iOS is the conversion between JSON strings and JSON objects. JSON strings and JSON objects are based on the NSObject class in iOS, and in interaction with the background, the NSObject class and the NSData class can be converted to each other for data interaction.
The conversion between the NSData and NSObject classes is simple and common in iOS frameworks and is not covered here, this article focuses on the conversion of JSON strings to JSON objects.
#==================== processing of data received from the background ===================================
The data received from the background is typically of type nsdata and can be parsed directly with the JSON parsing class provided by the iOS framework, and the JSON object is directly obtained.
Get the string data of interface feedback, here is the Mknetworkkit network library nsstring *responsestring = [Completedoperation responsestring]; NSData *responsedata = [responsestring datausingencoding:nsutf8stringencoding];if (responseData! = nil) { ID Jsonobject = nil; Nserror *error = nil; Jsonobject = [Nsjsonserialization jsonobjectwithdata:responsedata options:nsjsonreadingmutableleaves error:& ERROR]; If (error = = ill) { //determine jsonobject as Nsarray or Nsdictionary } else { //json parsing error handling }}
If the key-value pair returned in the background contains a JSON string type, then the JSON strings will be parsed
+ (ID) Jsonobjectwithstring: (NSString *)string{NSData*data = [stringdatausingencoding:nsutf8stringencoding]; Nserror*error; IDJsonobject = [Nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves error:&ERROR]; if(Error = =Nil) { returnJsonobject; } Else{DLog (@"%@", error.localizeddescription); returnNil; }}
#==================== The front end is passed to the background JSON format processing ===================================
If you need to pass JSON data to the background when you call the interface in the background, you typically convert the JSON Nsarray nsdictionary type to the NSString type, and then you can choose to pass the key value to the parameter or NSData binary format to the background.
JSON collection object converted to JSON string code
+ (NSString *) Jsonstringwithobject: (ID) jsonobj{NSString*jsonstring =Nil; Nserror*error; NSData*jsondata =[nsjsonserialization datawithjsonobject:jsonobj options:0Error:&ERROR]; if(!jsondata) {DLog (@"Got An error:%@", error); } Else{jsonstring=[[NSString alloc] Initwithdata:jsondata encoding:nsutf8stringencoding]; } returnjsonstring;}
Web data JSON processing for iOS development