Use json in IOS and json in IOS

Source: Internet
Author: User
Tags allkeys

Use json in IOS and json in IOS

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

2. decompress the downloaded package and import all files in the class folder to the current project.

3. Add the import Statement to the file used: # import "SBJson. h"

4. Convert the json string into an NSDictionary object.

Cpp Code
  • NSString * temp = @ "{\" 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];
  • NSString * temp = @ "{\" 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. recursively traverse the parsed NSDictionary object

     

    Cpp Code
  • -(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];
  • }
  • }
  • }
  • -(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. Restore the parsed NSDictionary object to a json string

    Cpp Code
  • NSString * jsonStr = [items JSONRepresentation];
  • NSString * jsonStr=[items JSONRepresentation];

     


    IOS development, how to use json files

    JSON is generally a piece of data requested from the network interface. first, you need to send a request to the server, get a piece of JSON, and then parse it. two third-party open source class libraries, ASIHTTPRequest and SBJSON, are used.

    NSURL * url = [NSURL URLWithString: [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    SendRequest = [ASIHTTPRequest requestWithURL: url];

    [SendRequest setTimeOutSeconds: 30];

    [SendRequest setDelegate: self];

    [SendRequest startAsynchronous];

    -(Void) requestFinished :( ASIHTTPRequest *) request

    {

    NSString * responseString = [request responseString];

    If (responseString = nil | [responseString JSONValue] = nil ){

    Return;

    }

    NSDictionary * responseDict = [responseString JSONValue];

    Int result = [[responseDict objectForKey: @ "status"] intValue];

    If (result = 1 ){

    NSArray * location = [responseDict objectForKey: @ "locations"];

    ...............................

    }

    How to get a json package in the ios path

    NSString * strPath = [[NSBundle mainBundle] pathForResource: @ "json file name" ofType: @ "json"]; NSString * str = [NSString stringWithContentsOfFile: strPath encoding: NSUTF8StringEncoding error: nil];

    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.