简体   繁体   中英

How to iterate over json array

I have this json code

{
  "apps": [
    {
      "com.eeenmachine.tinytowers": [
        {
          "text": "one"
        },
        {
          "text": "two"
        }
      ]
    }
  ]
}

And i am trying to get all the text values.

Trying with this code

NSArray *titles = [jsArray[@"apps"] valueForKey:game];
        NSLog(@"[DEBUG]titles %@",titles);
        for (id obje in titles){
            NSLog(@"[DEBUG]obje %@",obje);
            NSLog(@"[DEBUG]obje_class %@",[obje class]);
        }

Problem is that I don't get each text value , instead i get all the values.

Output:

obje (
            {
            text = one;
        },
            {
            text = two;
        }
    )

Looking at your sample data there are a lot of arrays ( [ , ] ) as well as dictionaries ( { , } ). For example the value for key apps is an array containing a single dictionary. Your code does not appear to be dealing with these extra levels.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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