我正在尝试使用一些疯狂的网络服务。部分服务返回一个看起来像这个屏幕截图的值。
下面是我的代码只是试图循环NSDictionary。
NSDictionary *platesJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for(id key in platesJson) {
id myvalue = [platesJson objectForKey:key];
NSString *myValue = [[platesJson objectForKey:key] objectForKey:@"value"];
}
我已经尝试循环遍历数组字典并提取信息,但似乎总是返回nil或完全崩溃。具有讽刺意味的是,屏幕拍摄的值为0是这个Web服务的错误,我试图将if条件置于其中。
我也尝试了以下结果,因此也没有;
for (int i = 0; i< platesJson; i++)
{
NSString *myValue = [[platesJson objectAtIndex:i] objectForKey:@"value"];
}
if(myValue == 0){
//Alert the user no data found
}
else{
//Continue on....
}
我确信它的一些非常简单,只是坚持使用正确的语法来处理它的最佳方法。
答案 0 :(得分:1)
NSDictionary *platesJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for(id key in platesJson) {
id myvalue = [platesJson objectForKey:key];
NSString *myValue = [platesJson objectForKey:key];
}
我想这会奏效,
[platesJson objectForKey:key]
会获得价值。如果值是Dictionary,则需要再次循环。但是在你发布的图片中,它只是一个简单的词典
答案 1 :(得分:0)
如果您确实确定密钥的值为NSDictionary
,则可以获得以下内容:
NSDictionary *myValue = JSON[ key ];
你可以从myValue
得到完全相同的值:
NSString *someString = myValue[ @"KLSL" ];
注意JSON中的第一个元素是NSArray或NSDictionary。
我希望这可以帮到你。