如何在objective-c中传递此JSON的值?

时间:2017-10-10 04:21:41

标签: objective-c json

weather =     (
            {
        description = mist;
        icon = 50n;
        id = 701;
        main = Mist;
    },
            {
        description = "heavy intensity drizzle";
        icon = 09n;
        id = 302;
        main = Drizzle;
    }
)

我想要获取" main"由此, 我正在使用此

这样做
NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSDictionary *dic4 = [dic1 objectForKey:@"weather"];

NSString * weatherStr =  
    [NSString stringWithFormat:@"Weather type:%@",[dic4 valueForKey:@"main"]];
weathertypeLabel.text =  
    [NSString stringWithFormat:@"Weather type:%@",weatherStr];

但我的o / p是

Weather type:(
    Mist,
    Drizzle
)

2 个答案:

答案 0 :(得分:2)

当你这么说时,你只是在开玩笑:

sudo service networking restart

尽管您声明了,NSDictionary *dic4 = [dic1 objectForKey:@"weather"]; 不是字典。它是一个字典数组(NSArray),包含两个字典。因此,您一次从中提取dic4键的值

如果那不是你想要的,那么你需要决定你想要的词典,"main"dic4[0]。解压缩,然后获取其dict4[1]

答案 1 :(得分:0)

NSString * weatherStr =  
[NSString stringWithFormat:@"Weather type:%@",[dic4 valueForKey:@"main"]];

使用此

 NSString * weatherStr =  
[NSString stringWithFormat:@"Weather type:%@",[[dic4 objectForKey:@"weather"]objectForKey :@"main"]];