【问题标题】:objective c how to get the specific json key value in ios目标c如何在ios中获取特定的json键值
【发布时间】:2019-04-29 00:40:19
【问题描述】:

我有全国各地相关 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;
},

【问题讨论】:

  • 尝试根据 dial_code 获取,因为它是静态的 OR if id == 4 { } else { }。
  • 感谢您的回复 dial_code 不是静态的。我想动态获取标志名称.. 我的意思是不是特定的标志名称
  • let array.first(where: { guard let name = $0["name"] else { return false }; name == "Algeria" })?
  • 非常感谢您的回复。我正在使用目标 c 。请在目标 c 中给出示例代码。我想动态获取国旗名称。不是静态的
  • 目前还不清楚您可以使用什么值进行过滤。是“阿尔及利亚”吗?是“3”吗?但是indexOfObjectPassingTest: 可以帮助您做到这一点。

标签: ios objective-c json xcode


【解决方案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"];

【讨论】:

  • 您好,感谢您的回复。但我想动态地做兄弟..不是静态的。你能来聊天吗..我想和你聊天..
  • 动态是什么意思。你想怎么做搜索?您的具体要求是什么。
  • 你能创建聊天室吗..我有一些疑问兄弟..请
  • [NSNumber numberWithInteger:3] 在这里我需要将值指定为动态而不是静态..
  • 对于 id,我有一个数组。例如,我想显示印度国旗,然后我需要获取 id 值并传递给 nspredicate bro。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多