Search code examples
iosobjective-cnsdictionaryunirest

Objective-c parsing JSON


I'm totally new in Objective-c programming.

I try to make a get request from my app. I'm using Unirest for Objective-C as the HTTP Libraries.

My API will return JSON as below :-

{
  "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.

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 :-

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.


Solution

  • 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.

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