2

Perhaps I am over-thinking or confusing myself, but my head is stuck in a loop over this and I cannot break out.

I have a a JSON of the format: (Validated at http://jsonformatter.curiousconcept.com/)

{
  id: 1,
  sections: "5",
  total: "10",
  result: {
    3: 00PM: [
      {
        name: "Anurag",
        status: "Web"
      },
      {
        name: "Anurag2",
        status: "Web2"
      }
    ],
    5: 00PM: [
      {
        name: "Anurag",
        status: "Seated"
      }
    ],
    6: 00PM: [
      {
        name: "Anurag4",
        status: "Web4"
      },
      {
        name: "Anurag5",
        status: "Web5"
      },
      {
        name: "Anurag6",
        status: "Web6"
      },
      {
        name: "Anurag7",
        status: "Web7"
      }
    ]
  }
}

I have this so far:

        NSDictionary *dict = [response JSONValue];
        NSDictionary *results = [dict objectForKey:@"result"];

        NSInteger num_results = [[dict valueForKey:@"total"] intValue];
        NSInteger num_sections = [[dict valueForKey:@"sections"] intValue];

        NSMutableArray *sections = [[NSMutableArray alloc] initWithCapacity:num_sections];
        NSMutableArray *objarr = [[NSMutableArray alloc] initWithCapacity:num_results];
        NSMutableArray *obj= [[NSMutableArray alloc] initWithCapacity:num_sections];
        NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:num_results];

        for (NSString* key in results) {
            NSLog(@"Key: %@", key); // prints out 3:00 PM...5:00 PM etc...
            [obj addObject:[results objectForKey:key]]; // nested objects within each key are saved in the array
            NSLog(@"Object 1: %@", obj);
        }

        for (int i = 0; i < [obj count]; i++) {
            //NSLog(@"Object 2: %@", [obj objectAtIndex:i]);
            [temp addObject:[obj objectAtIndex:i]]; // I take each object from previous array and save it in a temp array
            for (int i = 0; i < num_results; i++) {
                NSLog(@"Object 3: %@", [temp objectAtIndex:i]);
                **[objarr addObject:[temp objectAtIndex:i]]; // I want to extract the object within the object but cannot get it to work**
            }   
        }

I am able to make an array of objects within each of the 3 keys inside results. But I am not able to get each of the objects inside them as a separate object and save them in an array.

For example, in an NSArray I have:

Object 3: (
        {
        name = "Anurag ";
        status = Web;
    },
        {
        name = "Anurag ";
        status = Web;
    }
)

How can I get the two objects inside this object and save them both in an array? I guess the main issue is I cannot refer to a key name to get the individual object.

anuragbh
  • 603
  • 1
  • 5
  • 11
  • Your "Object 3" is an array containing two dictionaries. – Hot Licks Jul 06 '12 at 20:15
  • I think you are right. But how do I get its value? Inside object 3 I have two nested objects without keys. – anuragbh Jul 06 '12 at 20:26
  • Object 3 is an array (in fact, you created it) -- index it to get at the two dictionaries you put there. (Or you could cut out the middleman and process the dictionaries you're getting out of `obj` immediately.) (Hint: Use more meaningful variable names, so you understand what you're doing.) – Hot Licks Jul 06 '12 at 20:54

3 Answers3

4

You can use the following code,

NSDictionary *json = [response JSONValue];

// Get the objects you want
NSArray *items = [json valueForKeyPath:@"result.6: 00PM"];

This will return an NSArray with the objects for the key 6: 00PM

Please check this link too.

Community
  • 1
  • 1
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Thanks, but I couldn't know the value "6:00 PM". Thats why in my code I iterate over the entire results array using its index. That is not the issue though, I am able to extract that object. It is the nested objects within that object I need to get. – anuragbh Jul 06 '12 at 20:16
  • i think you need to get the `{ name = "Anurag "; status = Web; }` and `{ name = "Anurag "; status = Web; }` objects in an array, is it ? – Midhun MP Jul 06 '12 at 20:21
  • Yes, all of them in a single array – anuragbh Jul 06 '12 at 20:23
  • my answer will give you the two objects of the Key `6:00PM` in an array. Same like **@"result.6: 00PM"** you need to use other keys, that will return corresponding arrays. At last you need to add these `NSArray` objects to a single `NSMutableArray` – Midhun MP Jul 06 '12 at 20:40
  • Thanks @Midhun, it works, but I wouldn't know the value of the key coming in. Like 6: 00PM etc. Is there any way I can refer to that? – anuragbh Jul 06 '12 at 20:52
  • 1
    @anuragbh -- You need to decide what your "target" data structure is and work toward that, rather than just tossing the data around and hoping it lands in a form that will suit you. THINK about how the data must be organized to do whatever it is you ultimately intend to do with the data. – Hot Licks Jul 06 '12 at 20:58
  • @anuragbh: then only give `result` in the above code, instead of `result.6: 00PM`. This will give you an array of arrays, those arrays will have the objects you want. – Midhun MP Jul 06 '12 at 21:06
  • @HotLicks Thanks I will return to this and organize and label the code better. – anuragbh Jul 06 '12 at 21:10
  • @MidhunMP Thanks, that is what I had in object 3 before. I was not able to extract the arrays from it. But I think I have a lead from your answer. I will iterate over the array and get the values for the keys I need. – anuragbh Jul 06 '12 at 21:11
1

You are redefining int i in your second for loop. Try something like

for(int j = 0; j < num_results; j++)
pdesantis
  • 879
  • 6
  • 7
1
[temp addObject:[obj objectAtIndex:i]]; // I take each object from previous array and save it in a temp array

This is useless motion. You're effectively just copying the obj array to the temp array -- no value added.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151