Using JSON in iOS

Source: Internet
Author: User

1. Download the JSON framework from https://github.com/stig/json-framework/: json-framework

2. Unzip the downloaded package and import all the files under the class folder into the current project.

3, add the import statement in the file used:#import "SBJson.h"

4. Convert the JSON string to a Nsdictionary object.

 

CPP Code
    1. nsstring *[email protected] "{\" China \ ": {  
    2.                            \ "Beijing \": {\ "Beijing 1\": 1,\ "Beijing 2\": 2,\ "Beijing 3\": 3},  
    3.                           \ "shanghai \": {\ " Shanghai 1\ ": 4,\" Shanghai 2\ ": 5,\" Shanghai 3\ ": 6},  
    4.                           \ "guangzhou \": {\ "guangzhou 1\": 7,\ "Guangzhou 2\": 8,\ "Guangzhou 3\": 9}} ";   
    5. Nsdictionary *items  = [temp jsonvalue];  
NSString *[email protected] "{\" China \ ": {\" beijing \ ": {\" Beijing 1\ ": 1,\" Beijing 2\ ":                           2,\" Beijing 3\ ": 3},                          \" Shanghai \ ": {\" Shanghai 1\ ": 4,\" Shanghai 2\ " : 5,\ "Shanghai 3\": 6},                          \ "guangzhou \": {\ "guangzhou 1\": 7,\ "Guangzhou 2\": 8,\ "Guangzhou 3\": 9}} "; Nsdictionary *items = [temp Jsonvalue];

5. Recursive traversal of parsed nsdictionary objects

CPP Code
  1. -(void) Visitdict: (nsdictionary *) dict{
  2. Nsarray *keys=[dict AllKeys];
  3. For (NSString *key in keys) {
  4. NSString *result=[nsstring stringwithformat:@"key=%@,value=%@", Key,[dict Objectforkey:key]];
  5. NSLog (result);
  6. if ([[[Dict Objectforkey:key] iskindofclass:[nsdictionary class]) {
  7. [Self visitdict:[dict objectforkey:key]];
  8. }
  9. }
  10. }
-(void) Visitdict: (nsdictionary *) dict{  nsarray *keys=[dict AllKeys];  For (NSString *key in keys) {     nsstring *result=[nsstring stringwithformat:@ "key=%@,value=%@", key,[dict Objectforkey:key]];     NSLog (result);     if ([[[Dict Objectforkey:key] Iskindofclass:[nsdictionary class]) {            [self visitdict:[dict objectforkey:key]];     }   }}

6. Revert the parsed Nsdictionary object to a JSON string

 

CPP Code
    1. NSString * Jsonstr=[items jsonrepresentation];
NSString * Jsonstr=[items jsonrepresentation];

Using JSON in iOS

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.