Common Methods of iOS JSONKit

Source: Internet
Author: User

During project interaction with the server, json Parsing is often required. If someone wants to parse JSON, JSONKit can be a good choice.


You can download JSONKit from gitHub at: JSONKit


After the project is added, set JSONKit to a mode that does not support arc.



Then, modify the two places at the point m. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + placement = "brush: java;"> // array-> isa = _ JKArrayClass; object_setClass (array, _ JKArrayClass); // dictionary-> isa = _ JKDictionaryClass; object_setClass (dictionary, _ JKDictionaryClass );This is because isa has been deprecated. Therefore, call the object_setClass () and object_getClass () functions.

Two json strings are set here for your reference.

Arr. conf

[{"preview_sub": "adornment_fly_1_100.png", "width": "300", "path": "adornment_fly_1.png", "name": "adornment_fly_1.png", "height": "300"}, {"preview_sub": "adornment_fly_2_100.png", "width": "300", "path": "adornment_fly_2.png", "name": "adornment_fly_2.png", "height": "300"}]

Dict. conf

{"status": "ok", "after": "14851", "before": "11011"}

NSString * res = nil; // array to json string NSArray * arr = [[NSArray alloc] initWithObjects: @ "One", @ "Two", @ "Three ", nil]; res = [arr JSONString]; NSLog (@ "res = % @", [NSString stringWithString: res]); // dictionary type (object) convert the json string NSArray * arr1 = [NSArray arrayWithObjects: @ "dog", @ "cat", nil]; NSArray * arr2 = [NSArray arrayWithObjects: [NSNumber numberWithBool: YES], [NSNumber numberWithInt: 30], nil]; NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: arr1, @ "pets", arr2, @ "other", nil]; res = [dic JSONString]; NSLog (@ "res = % @", [NSString stringWithString: res]); // json string to array NSString * arrConf = [[NSString alloc] handler: [[NSBundle mainBundle] pathForResource: @ "arr" ofType: @ "conf"] encoding: NSUTF8StringEncoding error: nil]; NSArray * ConfArr = [arrConf objectFromJSONString]; NSLog (@ "% @", ConfArr); // json string to dictionary NSString * dicConf = [[NSString alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "dict" ofType: @ "conf"] encoding: NSUTF8StringEncoding error: nil]; NSArray * ConfDic = [dicConf objectFromJSONString]; NSLog (@ "% @", ConfDic );


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.