iOS development-use Nsjsonserialization to parse JSON data

Source: Internet
Author: User

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

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.