IOS JSON从“JSON Dict”中获取所有值。 [英] IOS JSON get all values from a "JSON Dict"

查看:580
本文介绍了IOS JSON从“JSON Dict”中获取所有值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据结构:

{ 
  "artistlist " : [ 
                    {
                      "performer" : "Gate Zero"
                    },
                    {
                      "performer" : "nightech"
                    },
                    {
                      "performer" : "Marko Fuerstenberg"
                    },

                  ] 
   }

我用这行代码从NSString读取这个结构到NSDictionary:

I read this structure from NSString into NSDictionary with this line of code:

   JSON = [NSJSONSerialization JSONObjectWithData:
   [[chunks objectAtIndex:1]        
   dataUsingEncoding:NSUTF8StringEncoding] options:              
   NSJSONReadingMutableContainers error: &e];

with: [JSON objectForKey:@artistlist] 我得到这种结构:

with: [JSON objectForKey:@"artistlist "] i get this structure:

(
    {
    performer = "Gate Zero";
},
    {
    performer = nightech;
},
    {
    performer = "Marko Fuerstenberg";
}
)

有没有什么方法可以更深入?

Is there any way to go "deeper" ?

我将如何解析生成的结构?

how would i parse the resulting Structure ?

我想直接获取值列表或访问执行者名称。如果我在tupel中有多个值,例如表演者姓名,专辑,年份,该怎么办?我如何访问这些值?

I would like to get a list of values or access performer names directly. What if i have several values in a tupel for example performer name, album, year. How would i access those values?

谢谢。

推荐答案

是的,在您拥有 [JSON objectForKey:@artistlist] 之后,您将获得 NSArray NSDictionaries (有点令人困惑!)。

Yes, after you have [JSON objectForKey:@"artistlist "], you get an NSArray of NSDictionaries (slightly confusing!).

NSArray *performersArray = [JSON objectForKey:@"artistlist"];  
for (NSDictionary *performerDic in performersArray) {
    NSLog(@"%@", [performerDic objectForKey:@"performer"]);
}

这应该产生每个表演者的名字。或者,您可以执行(NSUInteger i = 0; i< [performersArray count]; i ++)并访问 NSDictionary * performersDic = [performersArray objectAtIndex :i] 。从那里,你可以类似地使用 [performDic objectForKey:@performer]

This should yield each performer name. Alternatively, you can do for (NSUInteger i = 0; i < [performersArray count]; i++) and access NSDictionary *performersDic = [performersArray objectAtIndex: i]. From there, you can similarly use [performsDic objectForKey:@"performer"]

这篇关于IOS JSON从“JSON Dict”中获取所有值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆