【问题标题】:How to search JSON element using SwiftyJSON in Swift 3如何在 Swift 3 中使用 SwiftyJSON 搜索 JSON 元素
【发布时间】:2018-02-14 09:52:42
【问题描述】:

我在这里搜索了一些小例子来展示如何在 SwiftyJSON 中搜索 json 元素。我有以下 json 结构,有人可以给我技术吗?谢谢。

var jsonData = {
      "css":[
         {
            "path": "style.css",
            "updated": "12432"
         },
         {
            "path": "base.css",
            "updated": "34627"
         }
      ],
      "html":[
         {
            "path": "home.htm",
            "updated": "3223"
         },
         {
            "path": "about",
            "updated": "3987"
         }
      ]
    }

我想搜索“html”->“home.htm”行。如何为它使用过滤方法。

对于简单的 json 结构,我得到了这样的示例,但就我而言,我不知道。

{
    countryid : "1"
    name : "New York"
},
{
    countryid : "2"
    name : "Sydeny"
}

if let array = arrCountry.arrayObject as? [[String:String]],
   foundItem = array.filter({ $0["name"] == "def"}).first {
   print(foundItem)
}

【问题讨论】:

  • 您想在您提供的 JSON 中搜索 htmlhome.htm 的值吗?所以你正在寻找updated 值?
  • 是的,我想快速搜索“home.htm”并获取“更新”值。这是因为我有另一个 json 数据相同的结构来比较这个元素存在并比较“更新”值

标签: ios json swift3 swifty-json


【解决方案1】:
let updated = json["html"].array?.filter { $0["path"].string == "home.htm" }.first?["updated"].string
print(updated)

// OR

for subJson in json["html"].arrayValue where subJson["path"].stringValue == "home.htm" {
    let updated  = subJson["updated"].stringValue
    print(updated)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-01
    • 2018-09-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多