ios json TouchJSON

参考地址为:https://github.com/TouchCode/TouchJSON

 

Open经验库 

 

网络中数据的传输往往是以Json或XML格式进行的,如新浪微博、腾讯微博、人人网等。今天主要介绍一下ios下Json数据的解析。

首先是使用TouchJson进行解析:

TouchJson点击下载

首先导入

将Source文件加下的所以文件导入工程,在合适的位置添加CJSONDeserializer.h头文件

代码:

01NSString *jsonStr = <p class="p1">@"{\"customer\":[{\"name\":\"roamer\",\"ycount\":\"232.4\",\"sumcount\":\"322.3\"},{\"name\":\"<span class="s1">王三</span>\",\"ycount\":\"221.2\",\"sumcount\":\"1123.2\"},{\"name\":\"<span class="s1">李四</span>\",\"ycount\":\"1221.2\",\"sumcount\":\"12123.2\"}]}"<spanclass="s2">;</span></p>; 
02    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; 
03    NSError *error; 
04    NSMutableDictionary *root = [[CJSONDeserializer deserializer] deserialize:jsonData error:&error]; 
05    NSLog(@"Customers:%@", root); 
06    NSMutableArray *customers = [root objectForKey:@"customer"]; 
07    NSString *jsonTextStr = @"姓名:\n"
08    for (NSMutableDictionary *customer in customers) { 
09        NSString *name = [customer objectForKey:@"name"]; 
10        name = [name stringByAppendingString:@"\n"]; 
11        jsonTextStr = [jsonTextStr stringByAppendingString:name]; 
12        NSLog(@"customer:%@",[customer objectForKey:@"name"]); 
13    }
输出结果:
01Customers:{ 
02    customer =     ( 
03                
04            name = roamer; 
05            sumcount = "322.3"; 
06            ycount = "232.4"; 
07        }, 
08                
09            name = "\U738b\U4e09"; 
10            sumcount = "1123.2"; 
11            ycount = "221.2"; 
12        }, 
13                
14            name = "\U674e\U56db"; 
15            sumcount = "12123.2"; 
16            ycount = "1221.2"; 
17        
18    ); 
19
202012-06-15 14:08:20.406 TouchJsonTest[1495:f803] customer:roamer 
212012-06-15 14:08:20.406 TouchJsonTest[1495:f803] customer:王三 
222012-06-15 14:08:20.406 TouchJsonTest[1495:f803] customer:李四
下面使用SBJson

SBJson下载
首先导入

将Classes文件加下的所以文件导入工程,在合适的位置添加SBJson.h头文件

代码:

01//解析数据 
02    SBJsonParser *parser = [[SBJsonParser alloc] init]; 
03    NSString *jsonStr = <p class="p1">@"{\"customer\":[{\"name\":\"roamer\",\"ycount\":\"232.4\",\"sumcount\":\"322.3\"},{\"name\":\"<span class="s1">王三</span>\",\"ycount\":\"221.2\",\"sumcount\":\"1123.2\"},{\"name\":\"<span class="s1">李四</span>\",\"ycount\":\"1221.2\",\"sumcount\":\"12123.2\"}]}"<spanclass="s2">;</span></p> 
04    NSError *error = nil; 
05    NSMutableDictionary *root = [[NSMutableDictionary alloc] initWithDictionary:[parser objectWithString:jsonStr error:&error]]; 
06    //转换代码 不然会带来中文问题,诸如加()、:变成 = 等乱码问题 
07    SBJsonWriter *jsonWrite = [[SBJsonWriter alloc] init]; 
08    NSString *jsonString = [jsonWrite stringWithObject:root]; 
09    NSLog(@"JsonString:%@", jsonString); 
10    [jsonWrite release]; 
11    NSMutableArray *customers = [root objectForKey:@"customer"]; 
12    NSLog(@"Customers:%@", customers); 
13       
14    for (NSMutableDictionary *customer in customers) { 
15        NSLog(@"Customer-name:%@", [customer objectForKey:@"name"]); 
16    
17    [self.jsonData setText:@"解析完成"]; 
18    [self.parseBtn setTitle:@"Done" forState:UIControlStateNormal];
使用TouchJson和SBJson都可以快速的完成Json数据的解析,两者使用起来都比较简单,但TouchJson性能更好一些推荐使用。
转自:http://blog.csdn.net/shang_515/article/details/7666261 

posted on 2012-09-04 11:37  wtq  阅读(1348)  评论(0编辑  收藏  举报