在做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":"xinjiangqingzhen"},{"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 : benbangcai2013-07-23 21:16:28.962 quhaoIOS[832:c07] value2 is : 10002013-07-23 21:16:28.963 quhaoIOS[832:c07] value1 is : hanguoliaoli2013-07-23 21:16:28.963 quhaoIOS[832:c07] value2 is : 4782013-07-23 21:16:28.963 quhaoIOS[832:c07] value1 is : huoguo2013-07-23 21:16:28.963 quhaoIOS[832:c07] value2 is : 9992013-07-23 21:16:28.964 quhaoIOS[832:c07] value1 is : mianbaodangao2013-07-23 21:16:28.964 quhaoIOS[832:c07] value2 is : 10002013-07-23 21:16:28.964 quhaoIOS[832:c07] value1 is : ribenliaoli2013-07-23 21:16:28.964 quhaoIOS[832:c07] value2 is : 9982013-07-23 21:16:28.965 quhaoIOS[832:c07] value1 is : tianpinyinpin2013-07-23 21:16:28.965 quhaoIOS[832:c07] value2 is : 10002013-07-23 21:16:28.965 quhaoIOS[832:c07] value1 is : xiangcai2013-07-23 21:16:28.966 quhaoIOS[832:c07] value2 is : 5842013-07-23 21:16:28.966 quhaoIOS[832:c07] value1 is : chuancai2013-07-23 21:16:28.966 quhaoIOS[832:c07] value2 is : 9992013-07-23 21:16:28.966 quhaoIOS[832:c07] value1 is : dongnanyacai2013-07-23 21:16:28.967 quhaoIOS[832:c07] value2 is : 2602013-07-23 21:16:28.967 quhaoIOS[832:c07] value1 is : haixian2013-07-23 21:16:28.967 quhaoIOS[832:c07] value2 is : 3112013-07-23 21:16:28.967 quhaoIOS[832:c07] value1 is : shaokao2013-07-23 21:16:28.968 quhaoIOS[832:c07] value2 is : 10002013-07-23 21:16:28.968 quhaoIOS[832:c07] value1 is : xiaochikuaican2013-07-23 21:16:28.968 quhaoIOS[832:c07] value2 is : 9842013-07-23 21:16:28.969 quhaoIOS[832:c07] value1 is : xican2013-07-23 21:16:28.969 quhaoIOS[832:c07] value2 is : 9992013-07-23 21:16:28.969 quhaoIOS[832:c07] value1 is : xinjiangqingzhen2013-07-23 21:16:28.969 quhaoIOS[832:c07] value2 is : 2102013-07-23 21:16:28.970 quhaoIOS[832:c07] value1 is : yuecaiguan2013-07-23 21:16:28.970 quhaoIOS[832:c07] value2 is : 7712013-07-23 21:16:28.970 quhaoIOS[832:c07] value1 is : zhongcancaixi2013-07-23 21:16:28.970 quhaoIOS[832:c07] value2 is : 10002013-07-23 21:16:28.971 quhaoIOS[832:c07] value1 is : zizhucan2013-07-23 21:16:28.971 quhaoIOS[832:c07] value2 is : 316