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];

ios 使用json的更多相关文章

  1. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  2. 【疯狂造轮子-iOS】JSON转Model系列之一

    [疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...

  3. iOS开源JSON解析库MJExtension

    iOS中JSON与NSObject互转有两种方式:1.iOS自带类NSJSONSerialization 2.第三方开源库SBJSON.JSONKit.MJExtension.项目中一直用MJExte ...

  4. **iOS发JSON请求中字符串加转义,返回的JSON去转义

    iOS中使用NSSerialization把对象转为JSON字符串后,多出来反斜杠的问题 http://segmentfault.com/q/1010000000576646 NSDictionary ...

  5. iOS 读取Json 代码

    保存一下iOS 读取Json的代码,留着以后Copy用,哈哈. NSString* path = [[NSBundle mainBundle] pathForResource: @"Sand ...

  6. IOS 解析Json数据(NSJSONSerialization)

    ● 什么是JSON ● JSON是一种轻量级的数据格式,一般用于数据交互 ● 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除 外) ● JSON的格式很像OC中的字典和数组 ...

  7. iOS下JSON反序列化开源库

    iOS下JSON字符串反序列化成对象.在正式的项目中比較常见.例如以下几个经常使用开源库.能够依据个人喜好任选其一: 1. JSONModel: https://github.com/icanzilb ...

  8. iOS解决json串中的NSNull类型

    iOS解决json串中的NSNull类型   后端返回的数据中总会出现一些NSNull类型,当我们一处理程序就会崩溃,因此想到把返回的数据中的NSNull类型全部转换成@""空字符 ...

  9. IOS中Json解析的四种方法

    作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...

  10. ios中json解析出现的null问题

    http://my.oschina.net/iq19900204/blog/408034 在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式. 问题现象 但是几个 ...

随机推荐

  1. Netty学习_Netty框架入门教程:Netty入门之HelloWorld实现

    我们可能都学过Socket通信/io/nio/aio等的编程.如果想把Socket真正的用于实际工作中去,那么还需要不断的完善.扩展和优化.比如很经典的Tcp读包写包问题,或者是数据接收的大小,实际的 ...

  2. GO -- socket读取内容

    func handleRead(conn net.Conn, done chan string) { for { buf := make([]) reqLen, err := conn.Read(bu ...

  3. hough变换检测直线和圆

    图像测量和机器视觉作业: 提取图像中的直线和点的位置坐标,将其按一定顺序编码存入一文本文件,并在原图像上叠加显示出来. 下午实验了一下: 程序环境:vs2013(活动平台为x64)+opencv3.1 ...

  4. OpenCV学习教程入门篇&lt;一、介绍&gt;

    OpenCV,是Inter公司开发的免费开源专门因为图像处理和机器视觉的C/C++库,英文全称是Open Source Computer Vision. 1. 可视化语言Matlab与OpenCV都能 ...

  5. angular - 安装 -1

    在阅读以下教程以前,请安装node,请先确保您的使用平台:Win.Mac.Linux 首次安装node以后,我们先检测版本 node -v npm -v 这样就代表安装成功,那么我们可以进入下一步了 ...

  6. Codeforces Round #277 (Div. 2)D(树形DP计数类)

    D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. c语言-完全背包问题

    完全背包问题 问题:有N种物品和一个容量为V的背包,每种物品都有无限件可用.第i种物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 分 ...

  8. mysql + php 中文乱码 全是? 解决方法

    在my.ini文件中找到[client]和[mysqld]字段,在下面均加上default-character-set=utf8,保存并关闭,重启服务器 在window下重启失败,这是因为你安装了高版 ...

  9. [2011山东ACM省赛] Binomial Coeffcients(求组合数)

    Binomial Coeffcients nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  10. The type List is not generic(转载)

    错误:The type List is not generic; it cannot be parameterized with arguments <Activity> 代码如下: pu ...