iOS解析Server端返回JSON数据

时间:2022-02-15 09:13:10

在做quhao APP架构时,后台Server端使用了Java,提供WebService,而iOS和Android作为移动客户端。在做数据交互时,Server端返回JSON格式数据。由于iOS SDK中提供的JSON数据解析实在太过麻烦,这里讲下iOS下如何使用SBJson库来解析JSON格式数据。

首先我发起了一个HTTP请求,得到一个response字符串,此字符串是JSON格式:

NSURL *url = [NSURL URLWithString:@"http://192.168.1.2:9081/allCategories"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *httpError = [request error];
NSString *response = @"";
if (!httpError) {
response = [request responseString];
// NSLog(@"%@", response);
}

返回的数据格式如下:

[{"count":1000,"cateType":"benbangcai"},{"count":478,"cateType":"hanguoliaoli"},{"count":999,"cateType":"huoguo"},{"count":1000,"cateType":"mianbaodangao"},{"count":998,"cateType":"ribenliaoli"},{"count":1000,"cateType":"tianpinyinpin"},{"count":584,"cateType":"xiangcai"},{"count":999,"cateType":"chuancai"},{"count":260,"cateType":"dongnanyacai"},{"count":311,"cateType":"haixian"},{"count":1000,"cateType":"shaokao"},{"count":984,"cateType":"xiaochikuaican"},{"count":999,"cateType":"xican"},{"count":210,"cateType":"*qingzhen"},{"count":771,"cateType":"yuecaiguan"},{"count":1000,"cateType":"zhongcancaixi"},{"count":316,"cateType":"zizhucan"}]

我们可以看到JSON数据格式是非常整齐的,下面我们就使用SBJson来解析每一个数据块的2个值:count和cateType。

// 解析Server端返回的JSON数据
SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; //初始化SBJsonParser
NSError *error = nil;
NSArray *jsonObjects = [jsonParser objectWithString:response error:&error];
[jsonParser release], jsonParser = nil; for (NSDictionary *dict in jsonObjects) {
NSString *value1 = [dict objectForKey:@"cateType"];
NSString *value2 = [dict objectForKey:@"count"];
NSLog(@" value1 is : %@", value1);
NSLog(@" value2 is : %@", value2);
}

运行后打印结果:

2013-07-23 21:16:28.962 quhaoIOS[832:c07]  value1 is : benbangcai
2013-07-23 21:16:28.962 quhaoIOS[832:c07] value2 is : 1000
2013-07-23 21:16:28.963 quhaoIOS[832:c07] value1 is : hanguoliaoli
2013-07-23 21:16:28.963 quhaoIOS[832:c07] value2 is : 478
2013-07-23 21:16:28.963 quhaoIOS[832:c07] value1 is : huoguo
2013-07-23 21:16:28.963 quhaoIOS[832:c07] value2 is : 999
2013-07-23 21:16:28.964 quhaoIOS[832:c07] value1 is : mianbaodangao
2013-07-23 21:16:28.964 quhaoIOS[832:c07] value2 is : 1000
2013-07-23 21:16:28.964 quhaoIOS[832:c07] value1 is : ribenliaoli
2013-07-23 21:16:28.964 quhaoIOS[832:c07] value2 is : 998
2013-07-23 21:16:28.965 quhaoIOS[832:c07] value1 is : tianpinyinpin
2013-07-23 21:16:28.965 quhaoIOS[832:c07] value2 is : 1000
2013-07-23 21:16:28.965 quhaoIOS[832:c07] value1 is : xiangcai
2013-07-23 21:16:28.966 quhaoIOS[832:c07] value2 is : 584
2013-07-23 21:16:28.966 quhaoIOS[832:c07] value1 is : chuancai
2013-07-23 21:16:28.966 quhaoIOS[832:c07] value2 is : 999
2013-07-23 21:16:28.966 quhaoIOS[832:c07] value1 is : dongnanyacai
2013-07-23 21:16:28.967 quhaoIOS[832:c07] value2 is : 260
2013-07-23 21:16:28.967 quhaoIOS[832:c07] value1 is : haixian
2013-07-23 21:16:28.967 quhaoIOS[832:c07] value2 is : 311
2013-07-23 21:16:28.967 quhaoIOS[832:c07] value1 is : shaokao
2013-07-23 21:16:28.968 quhaoIOS[832:c07] value2 is : 1000
2013-07-23 21:16:28.968 quhaoIOS[832:c07] value1 is : xiaochikuaican
2013-07-23 21:16:28.968 quhaoIOS[832:c07] value2 is : 984
2013-07-23 21:16:28.969 quhaoIOS[832:c07] value1 is : xican
2013-07-23 21:16:28.969 quhaoIOS[832:c07] value2 is : 999
2013-07-23 21:16:28.969 quhaoIOS[832:c07] value1 is : *qingzhen
2013-07-23 21:16:28.969 quhaoIOS[832:c07] value2 is : 210
2013-07-23 21:16:28.970 quhaoIOS[832:c07] value1 is : yuecaiguan
2013-07-23 21:16:28.970 quhaoIOS[832:c07] value2 is : 771
2013-07-23 21:16:28.970 quhaoIOS[832:c07] value1 is : zhongcancaixi
2013-07-23 21:16:28.970 quhaoIOS[832:c07] value2 is : 1000
2013-07-23 21:16:28.971 quhaoIOS[832:c07] value1 is : zizhucan
2013-07-23 21:16:28.971 quhaoIOS[832:c07] value2 is : 316