2014-10-17 99 views
0

我完全是Objective-c編程的新手。Objective-c解析JSON

我嘗試從我的應用程序獲取請求。我使用Unirest作爲HTTP庫的Objective-C。

我的API將如下返回JSON: - 你看body.JSONObject [@ 「狀態」]

{ 
    "merchant_list": [ 
    { 
     "_id": "543ce2887ca4e102af44a5a8", 
     "address": "MyAddress bla bla bla", 
     "admin": [ 
     "543ce2887ca4e102af44a5a7" 
     ], 
     "card": null, 
     "circle": null, 
     "dollar_ratio": null, 
     "fb": null, 
     "logo": null, 
     "min_spending_VIP": null, 
     "modification_timestamp_utc": 1413276296.0, 
     "name": "Lorem ipsum", 
     "outlets": [], 
     "promotions": [], 
     "referral_ratio": null 
    } 
    ], 
    "status": "ok" 
} 

Unirest庫可以解析此JSON來的NSDictionary的NSArray或會給我 「OK」 的價值。

但問題是,我無法訪問merchant_list中的所有屬性。如下merchant_list的

UNIJsonNode *body = response.body; 
if ([body.JSONObject[@"status"] isEqualToString:@"ok"]) 
{ 
    NSLog(@"%@", body.JSONObject[@"merchant_list"]); 
} 

的NSLog值: -

2014-10-17 18:07:10.623 MyApp[18567:287513] (
     { 
     "_id" = 543ce2887ca4e102af44a5a8; 
     address = "MyAddress bla bla bla"; 
     admin =   (
      543ce2887ca4e102af44a5a7 
     ); 
     card = "<null>"; 
     circle = "<null>"; 
     "dollar_ratio" = "<null>"; 
     fb = "<null>"; 
     logo = "<null>"; 
     "min_spending_VIP" = "<null>"; 
     "modification_timestamp_utc" = 1413276296; 
     name = "Lorem ipsum"; 
     outlets =   (
     ); 
     promotions =   (
     ); 
     "referral_ratio" = "<null>"; 
    } 
) 

任何幫助嗎?我已經花了幾個小時來處理這個錯誤

+1

使用此lib目錄下:https://github.com/AFNetworking/AFNetworking – 2014-10-17 10:15:08

+0

屬性不能訪問哪些?您的日誌看起來好像與輸入的JSON匹配。 – Keab42 2014-10-17 10:15:40

+1

@SandroMachado將調查 – skycrew 2014-10-17 10:16:54

回答

0

我今天發燒,但我仍在工作。結果我甚至不能解決這個簡單的錯誤。

感謝Rajpal Thakur爲突出問題。

現在我可以訪問屬性: -

if ([body.JSONObject[@"status"] isEqualToString:@"ok"]) 
    { 
     for (id merchant in body.JSONObject[@"merchant_list"]) 
     { 
      for (id key in merchant){ 
       NSLog(@"key: %@, value: %@ \n", key, [merchant objectForKey:key]); 
      } 
     } 
    }