1.JSON data structure and parsing1.1JSON data StructureJson:javascript Object Notation (JavaScript object Notation). JSON is the syntax for storing and exchanging textual information. Similar to XML. JSON is smaller, faster, and easier to parse than XML. The JSON value can be either a numeric (integer or floating-point) string (in double quotes), a logical value (TRUE or false) (in square brackets) the object (in curly brackets) The null JSON object is written in curly braces: An object can contain multiple name/value pairs: The name and value are separated by ":", Similar to the dictionary in OC. {"FirstName": "John", "LastName": "Doe"} The JSON array is written in square brackets: The array can contain multiple objects: {"Employees": [{"FirstName": "John", "LastName": "Doe"},{"FirstName": "Anna", "LastName": " Smith "},{" FirstName ":" Peter "," LastName ":" Jones "}]} in the example above, the object" Employees "is an array containing three objects. Each object represents a record of a person (with a first and last name).1.2JSON parsing process (using nsjsonserialization)Files: Date_json.txt file contents: [
{
"Sender": "Sam",
"Receiver": "Jack",
"Content": see you on the playground tonight. ",
"Date": "October 19, 2015"
},
{
"Sender": "Bob",
"Receiver": "Marry",
"Content": see you on the playground tonight. ",
"Date": "October 19, 2015"
}]//Get file path
NSString *filepath=[[nsbundle mainbundle]pathforresource:@ "Date_json" oftype:@ "txt"];
Import a file into data
NSData * Data=[nsdata Datawithcontentsoffile:filepath];
Parsing with JSON
Nsarray * array=[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil];
Initializing an array
Self.dataarray=[nsmutablearray array];
Assigns the resulting data to the object and puts it into the array
For (nsdictionary *d in array) {
Message * M=[[message Alloc]init];
[M setvaluesforkeyswithdictionary:d];
[Self.dataarray addobject:m];
}
Show parsed documents
For (Message *m in Self.dataarray) {
NSLog (@ "\ n");
NSLog (@ "sender:%@", M.sender);
NSLog (@ "receiver:%@", m.receiver);
NSLog (@ "content:%@", m.content);
NSLog (@ "date:%@", m.date);
Note: #import "Message.h" @implementation Message
Undefined key to prevent KVC from crashing
If the corresponding key is not found, the method is executed-(void) SetValue: (ID) value Forundefinedkey: (NSString *) key{} @end Run Result:
2015-10-19 17:30:29.297 [3,182:264,087]
2015-10-19 17:30:29.297[3182:264087] Sender:sam
2015-10-19 17:30:29.298[3182:264087] Receiver:jack
2015-10-19 17:30:29.298[3182:264087] Content: I 'll see you on the playground tonight.
2015-10-19 17:30:29.298[3182:264087] date:2015 Year - Month - Day
2015-10-19 17:30:29.298[3182:264087]
2015-10-19 17:30:29.298[3182:264087] Sender:bob
2015-10-19 17:30:29.298[3182:264087] Receiver:marry
2015-10-19 17:30:29.298[3182:264087] Content: I 'll see you on the playground tonight.
2015-10-19 17:30:29.298[3182:264087] date:2015 Year - Month - Day
Source code Download (Xcode7.0.1)
iOS development-use Nsjsonserialization to parse JSON data