iOS解析Server端返回JSON資料

來源:互聯網
上載者:User

在做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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.