2017-09-06 105 views
1

我在这里搜索一个小例子来展示如何在SwiftyJSON中搜索json元素。我有以下json结构,任何人都可以技术我吗?谢谢。如何在Swift 3中使用SwiftyJSON搜索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) 
} 
+0

你想在你的价值观'html'然后'home.htm'提供的JSON搜索?所以你正在寻找'更新'的价值? – nathan

+0

是的,我想快速浏览“home.htm”并获取“更新”值。这是因为我有另一个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) 
}