目标C如何在iOS中获取特定的JSON键值

时间:2018-11-27 09:49:26

标签: ios objective-c json xcode

我对所有国家/地区相关的json都有json响应。我的问题是我想获得基于id键名的Algeria.png而没有使用图标名键。有人请指导我。我想动态获取图像名称,而不是静态获取。我正在使用目标C代码。

用于国家/地区数组的

json是:(

    {
    "dial_code" = "+93";
    "icon_name" = "Afghanistan.png";
    id = 1;
    name = Afghanistan;
},
    {
    "dial_code" = "+355";
    "icon_name" = "Albania.png";
    id = 2;
    name = Albania;
},
    {
    "dial_code" = "+213";
    "icon_name" = "Algeria.png";
    id = 3;
    name = Algeria;
},
    {
    "dial_code" = "+1684";
    "icon_name" = "American-Samoa.png";
    id = 4;
    name = "American Samoa";
},
    {
    "dial_code" = "+376";
    "icon_name" = "Andorra.png";
    id = 5;
    name = Andorra;
},
    {
    "dial_code" = "+244";
    "icon_name" = "Angola.png";
    id = 6;
    name = Angola;
},

1 个答案:

答案 0 :(得分:-1)

您有字典数组(这是JsonArray)。所以

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@",[NSNumber numberWithInteger:3]];
       (OR)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@",3];
NSArray *tempArray = [JsonArray filteredArrayUsingPredicate:predicate];

在tempArray中,您将获得字典对象。

NSDcitionary *dict = [tempArray objectAtIndex:0];
NSString *imageName = [dict valueForKey:@"icon_name"];