简体   繁体   English

Objective-C解析JSON

[英]Objective-c parsing JSON

I'm totally new in Objective-c programming. 我是Objective-c编程的新手。

I try to make a get request from my app. 我尝试从我的应用发出获取请求。 I'm using Unirest for Objective-C as the HTTP Libraries. 我正在将Unirest for Objective-C用作HTTP库。

My API will return JSON as below :- 我的API将返回JSON,如下所示:

{
  "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 library can parse this JSON to NSDictionary or NSArray as you see body.JSONObject[@"status"] will give me "ok" value. 如您所见,Unirest库可以将此JSON解析为NSDictionary或NSArray。JSONObject[@“ status”]将给我“确定”的值。

But the problem is, I'm unable to access all attributes in merchant_list. 但是问题是,我无法访问商人列表中的所有属性。

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

NSLog value of merchant_list as below :- 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>";
    }
)

Any help? 有什么帮助吗? I already spent hours for this error. 我已经花了几个小时来解决这个错误。

I having a bad fever today but im still working. 我今天发烧很厉害,但我仍在工作。 as result I cant even resolve this simple mistake. 结果,我什至无法解决这个简单的错误。

Thanks to Rajpal Thakur for highlighting the issue. 感谢Rajpal Thakur强调了这个问题。

now I can access the attributes :- 现在我可以访问属性了:

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]);
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM