how to read and write JSON files with JsonkitCategory: iOS2013-04-20 12:46510 people readComments (0)CollectionReport
JSON file format is simple, easy to use, worth a use.
At present, there are many libraries to support the parsing of JSON files, in this, Jsonkit easy to use and high efficiency, this article is about the use of Jsonkit.
1. Preparatory work
Download Jsonkit Package: http://download.csdn.net/detail/enuola/4523160
2. Write JSON file
Create file directory and file, need to use FileManager
[Plain]View Plaincopy
- Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
- NSString *documentsdirectory = [Paths objectatindex:0];
- Testdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "Data"];
- [FileManager createdirectoryatpath:testdirectory withintermediatedirectories:yes Attributes:nil Error:nil];
- Letterllkpath = [testdirectory stringbyappendingpathcomponent:@ "Letterllk.json"];
[Plain]View Plaincopy
- Open file, need to use Nsfilehandle
- Nsfilehandle *letterhandle = [Nsfilehandle Filehandleforwritingatpath:letterllkpath];
- Converting content to JSON data
The following data type conversions are supported in Jsonkit:
- NSString
- Nsarry
- Nsdictionary
It 's easy to use:
[Plain]View Plaincopy
- Raw data
- NSString *str = ....;
- Nsarry *arry = ....;
- Nsdictionary *dic = ...;
- Converting to a JSON string is the packaging of the raw data
- NSString *strjson = [str jsonstring];
- NSString *arryjson = [Arry jsonstring];
- NSString *dicjson = [dic jsonstring];
- or switch directly to nsdata*.
- NSData *strdata = [str jsondata];
- NSData *arrydata = [Arry jsondata];
- NSData *dicdata = [dic jsondata];
- Writes the converted JSON data to the text
[Plain]View Plaincopy< param name= "allowfullscreen" value= "false" >
- [Letterhandle Seektoendoffile];
- [Letterhandle Writedata:[strjson datausingencoding:nsutf8stringencoding];
- Or
- [Letterhandle Writedata:strdata];
3. Read the JSON file
[Plain]View Plaincopy< param name= "allowfullscreen" value= "false" >
- NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "test" oftype:@ "JSON"];
- NSString *jsonstring = [NSString stringwithcontentsoffile:filepath encoding:nsutf8stringencoding Error:nil];
- Parsing content with JSON
[Plain]View Plaincopy< param name= "allowfullscreen" value= "false" >
- //Based on a JSON file, parse by layer;
- nserror *error;
- id rawdata = [jsonstring objectfromjsonstringwithparseoptions:jkparseoptionnone error:&error];
-
- if ( error != nil ) {
- }
-
- if ( [rawData iskindofclass:[nsdictionary class]] )
- {}
- else if ( [rawData isKindOfClass:[NSArray class]] ) {
- }
- else {
- }
4. Summary
The encoding format of JSON mainly includes:
- [] represents the Nsarry class
- {} represents an object, such as Nsdictionary
- , representing the object delimiter
- : Represents a key-value pair, or can be understood as an assignment =
Jsonkit actually automatically helps you to add the original data to these symbols, or, conversely, to remove the symbols