0

I´ve got the next json code:

{

"person_1":{
      "name" :"person",
      "pictures":[ 
                  {"images":{
                             "picture_1": "url1",
                             "picture_2": "url1",
                            }
                  }
               ],
}
}

Can anybody tell me how to get the element "picture_1"? I am using touchJson for objective c.

Best Regards

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
DaSilva
  • 1,358
  • 1
  • 19
  • 42

1 Answers1

0

First, I would recommend using yajl as I found it very convenient for my work.

NSBundle * personBundle = [[yourstring yajl_JSON] valueForKey:@"person_1"];
NSArray * picturArray = [personBundle valueForKey:"pictures"];
NSBundle * imagesBundle = [picturArray objectAtIndex:0];
NSString * myUrl = [imagesBundle valueForKey:"picture_1"];

This should pretty much do it.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • I think you meant NSDictionary*, not NSBundle* – Firoze Lafeer May 09 '11 at 16:41
  • Using valueForKeyPath is cleaner and easier in this case. – nonamelive May 09 '11 at 16:43
  • @Fiorze Lafeer @nonamelive - I'm not an Objectie-C expert (not even close) so I showed it in the way I implement it, so first, thank for the comment, second, if you don't mind, please elaborate and third, I find it is hard to start with something new only with reading documentation and that an example helps many times, and since no one answered - I took the shot. Finally, if you have a better approach I would be **very very happy** to see your answers as well and learn from it. – MByD May 09 '11 at 16:48
  • First of all, thank you for the answer.. Its´s work!!! but I´ve to create another NSDictionary after the array like this: – DaSilva May 13 '11 at 14:04
  • NSDictionary *item_1 = [resultsDictionary objectForKey:@"person_1"]; NSArray *node_images =[item_1 objectForKey:@"pictures"]; NSDictionary *files = [[node_images objectAtIndex:0] objectForKey:@"images"]; NSString * stri = [files objectForKey:@"picture_1"]; NSString * stri = [files objectForKey:@"preview"]; – DaSilva May 13 '11 at 14:07