zoukankan      html  css  js  c++  java
  • ios 使用json

    1、从https://github.com/stig/json-framework/中下载json框架:json-framework

    2、解压下载的包,将class文件夹下的所有文件导入到当前工程下。

    3、在使用的文件中加入导入语句 :#import "SBJson.h"

    4、将json字符串转为NSDictionary对象

    [cpp]  view plain copy

    //测试json的解析  
    -(void)testJsonParser: (NSString *) jsonString  
    {  
        jsonString = [[NSString alloc] initWithString:@"{"userInfo":{"userName":"张三","sex":"男"}}"];  
        NSLog(@"正在解析json字符串是:%@",jsonString);  
          
        SBJsonParser * parser = [[SBJsonParser alloc] init];  
        NSError * error = nil;  
        NSMutableDictionary *jsonDic = [parser objectWithString:jsonString error:&error];  
        NSMutableDictionary * dicUserInfo = [jsonDic objectForKey:@"userInfo"];  
          
        NSLog(@"%@",[jsonDic objectForKey:@"userInfo" ]);  
        NSLog(@"%@",[dicUserInfo objectForKey:@"userName"]);  
        NSLog(@"%@",[dicUserInfo objectForKey:@"sex"]);  
    }  

    5、 处理json对象有多个记录的方法

    [cpp]  view plain copy

            NSString * customerGridJsonString = [[NSString alloc]initWithString:@"{"customer":[{"name":"roamer","ycount":"232.4","sumcount":"322.3"},{"name":"王三","ycount":"221.2","sumcount":"1123.2"},{"name":"李四","ycount":"1221.2","sumcount":"12123.2"}]}"];  
              
            SBJsonParser * parser = [[SBJsonParser alloc] init];  
    //        NSLog(@"%@",customerGridJsonString);  
            NSError * error = nil;  
              
            NSMutableDictionary *root = [[NSMutableDictionary alloc] initWithDictionary:[parser objectWithString:customerGridJsonString error:&error]];  
            NSLog(@"%@",root);  
            //注意转换代码  
            SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];  
              
            NSString *jsonString = [jsonWriter stringWithObject:root];  
              
            [jsonWriter release];  
            NSLog(@"%@",jsonString);  
            //注意转换代码  
            NSMutableArray * customers = [root objectForKey:@"customer"];  
            NSLog(@"%@",customers);  http://www.huiyi8.com/moban/
            for(NSMutableDictionary * member  in customers)  
            {  
                NSLog(@"%@",[[member objectForKey:@"name"] description]);  
            }  

    6、递归遍历解析出的NSDictionary对象

    [cpp]  view plain copy

    -(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]];    
         }    
       }    
    }    
    7、将解析出的NSDictionary对象还原为json字符串 
    [cpp]  view plain copy网站模板

    NSString * jsonStr=[items JSONRepresentation];  

  • 相关阅读:
    正则表达式记录
    XMLHttpRequest(Ajax)不能设置自定义的Referer
    删除右键菜单上的Adobe Drive CS4,Win7上也没有问题
    A HOWTO on Optimizing PHP(如何优化PHP的一篇文章)
    PHP XML To Array,将XML转换为数组
    让图片在高度确定的块元素中垂直居中
    (转)2011年,還是微軟IE的天下~網頁設計師哭泣吧!
    产生类似GUID的唯一ID
    转:编写跨浏览器兼容的 CSS 代码的金科玉律
    array_intersect 比 array_diff 快
  • 原文地址:https://www.cnblogs.com/xkzy/p/3812000.html
Copyright © 2011-2022 走看看