How IOS can read and write JSON files with Jsonkit

Source: Internet
Author: User

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 a file

Create file directory and file, need to use FileManager

[Plain]View Plaincopy
    1. Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
    2. NSString *documentsdirectory = [Paths objectatindex:0];
    3. Testdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "Data"];
    4. [FileManager createdirectoryatpath:testdirectory withintermediatedirectories:yes Attributes:nil Error:nil];
    5. Letterllkpath = [testdirectory stringbyappendingpathcomponent:@ "Letterllk.json"];
    • Open file ready to write
[Plain]View Plaincopy
    1. Open file, need to use Nsfilehandle
    2. 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
  1. Raw data
  2. NSString *str = ....;
  3. Nsarry *arry = ....;
  4. Nsdictionary *dic = ...;
  5. Converting to a JSON string is the packaging of the raw data
  6. NSString *strjson = [str jsonstring];
  7. NSString *arryjson = [Arry jsonstring];
  8. NSString *dicjson = [dic jsonstring];
  9. or switch directly to nsdata*.
  10. NSData *strdata = [str jsondata];
  11. NSData *arrydata = [Arry jsondata];
  12. NSData *dicdata = [dic jsondata];
    • Writes the converted JSON data to the text
[Plain]View Plaincopy< param name= "allowfullscreen" value= "false" >
    1. [Letterhandle Seektoendoffile];
    2. [Letterhandle Writedata:[strjson datausingencoding:nsutf8stringencoding];
    3. Or
    4. [Letterhandle Writedata:strdata];

3. Read the JSON file

    • Read File contents
[Plain]View Plaincopy< param name= "allowfullscreen" value= "false" >
    1. NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "test" oftype:@ "JSON"];
    2. NSString *jsonstring = [NSString stringwithcontentsoffile:filepath encoding:nsutf8stringencoding Error:nil];

    • Parsing content with JSON

[Plain]View Plaincopy< param name= "allowfullscreen" value= "false" >
    1. //Based on a JSON file, parse    by layer;
    2. nserror  *error;  
    3. id rawdata = [jsonstring  objectfromjsonstringwithparseoptions:jkparseoptionnone error:&error];  
    4.   
    5. if  ( error != nil )  {  
    6. }   
    7.   
    8. if  ( [rawData  iskindofclass:[nsdictionary class]] )    
    9. {}   
    10. else if  ( [rawData isKindOfClass:[NSArray class]] )  {  
    11. }  
    12. else {   
    13. }  

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

Related Article

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.