Iterating Though NSDictionary that has JSON Data

138 views Asked by At

So I've got an NSDictionary that holds JSON data from doing the following:

NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&someError];

And when I NSLog my dictionary, I get the output below.

2015-06-11 17:13:49.699 AmigoDash[57994:1195566] (
        {
        "_id" =         {
            "$id" = 5579fde2e4b0588ab009f841;
        };
        hours = "10-11";
        image = imageURL;
        name = "Boleskine Bistro";
    },
        {
        "_id" =         {
            "$id" = 5579fe1fe4b0588ab009f84a;
        };
        hours = "9-11";
        image = imageURL;
        name = Amrikos;
    }
)

The problem I'm facing now is trying to iterate through the dictionary. When I try to do it, the code crashes. I'm new to working with JSON and iOS in general. Kind of overwhelmed. I ultimately need to get the name and image information from each unique object in the dictionary, so I have to iterate through the dictionary.

2

There are 2 answers

2
Ganesh Somani On

You need to understand the basics of JSON components and how the are parsed into iOS. I will give you a simple example I got here. This I generally used for parsing JSON

{[
  {
    "array": [
      1,
      2,
      3
    ],
    "boolean": true,
    "null": null,
    "number": 123,
    "object": {
      "a": "b",
          "c": "d",
      "e": "f"
    },
    "string": "Hello World"
  },
  {
    "array": [
      1,
      2,
      3
    ],
    "boolean": true,
    "null": null,
    "number": 123,
    "object": {
      "a": "b",
      "c": "d",
      "e": "f"
    },
    "string": "Hello World"
  }
]}

This is the general kind of JSON you will get.

These are parsed as follows

  1. [] denote an array which is parsed into a NSArray
  2. {} denote an dictionary which is parsed into a NSDictionary
  3. true or false is a BOOL
  4. 123, 32, etc is int, NSInteger, NSNumber, etc
  5. The text with "" is for NSString

Now you should first pay attention to the structure, like what is inside what. And Then parse accordingly.

Let say for Above Example. The outermost object is an NSDictionary ({}).

 NSDictionary *cityJSON = [NSJSONSerialization JSONObjectWithData:**Above JSON Data**];

Inside we have an NSArray([])

NSArray *testFeeds = [NSJSONSerialization JSONObjectWithData: cityData options:NSJSONReadingMutableContainers error:nil];

Each of testFeeds object is an NSDictionary. This we we proceed from outermost to innermost object.

0
Leo On

From your log,your json is an array

I think using keyPath,it is easy

NSArray * array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&someError];
NSArray * namesArray = [array valueForKeyPath:@"name"]; //This will get a names array
NSArray * urlArray = [array valueForKeyPath:@"image"]; //This will get a url Array

If you still want to loop

NSArray * array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&someError];
for (NSDictionary * dic in array) {
    NSString * name = [dic valueForKey:@"name"];
    NSString * hours = [dic valueForKey:@"hours"];
    //So on
}