In the quhao app architecture, the backend server uses Java to provide WebService, while iOS and Android are mobile clients. During data interaction, the server returns data in JSON format. The JSON data parsing provided by the ios sdk is too troublesome. Here we will explain how to use the sbjson library in IOS to parse JSON data.
First, I initiate an HTTP request to get a response string in JSON format:
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); }
The returned data format is as follows:
[{"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"}]
We can see that the JSON data format is very neat. Next we use sbjson to parse the two values of each data block: Count and catetype.
// Parse the JSON data returned by the server. sbjsonparser * jsonparser = [[sbjsonparser alloc] init]; // initialize 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 );}
Print the result after running:
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