2017-09-06 4 views
1

Je cherche ici un petit exemple pour montrer comment rechercher un élément json dans SwiftyJSON. J'ai ci-dessous la structure de json, quelqu'un peut-il me tech? Merci.Comment rechercher un élément JSON en utilisant SwiftyJSON dans Swift 3

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

Je veux rechercher la ligne "html" -> "home.htm". Comment faire usage de la méthode de filtrage pour cela.

J'ai un exemple comme celui-ci pour une structure json simple, mais dans mon cas je n'en ai aucune idée.

{ 
    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

Vous voulez rechercher dans le JSON que vous avez fourni pour les valeurs '' html' puis home.htm'? Donc vous cherchez à obtenir la valeur 'updated'? – nathan

+0

Oui, je veux une recherche rapide à "home.htm" et obtenir la valeur "mise à jour". C'est parce que j'ai une autre structure de données json pour comparer cet élément existe et compare la valeur "mise à jour" –

Répondre

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) 
}