Swift,深入挖掘JSON

时间:2018-06-12 18:22:52

标签: json swift

以下是我使用的JSON:https://www.dropbox.com/s/1fm41jyv3rld72p/JSON.txt?dl=0

我成功使用此代码(谢谢OOPer!)深入挖掘并获取我需要的元素:

if
let dictionary = jsonData as? [String: Any],
let results = dictionary["results"] as? [[String: Any]],
//You need to choose one from "results"
!results.isEmpty, case let result = results[0],
let lexicalEntries = result["lexicalEntries"] as? [[String: Any]],
//You need to choose one from "lexicalEntries"
!lexicalEntries.isEmpty, case let lexicalEntry = lexicalEntries[0],
let pronunciations = lexicalEntry["pronunciations"] as? [[String: Any]],
//You need to choose one from "lexicalEntries"
!pronunciations.isEmpty, case let pronunciation = pronunciations[0],
let entries = lexicalEntry["entries"] as? [[String:Any]],
!entries.isEmpty, case let entry = entries[0],
let senses = entry["senses"] as? [[String:Any]],
!senses.isEmpty, case let sense = senses[0]

{
print (pronunciation)
print (entry)
print (sense)
}

但是当我尝试从JSON获取定义时,一切都崩溃了。我试过这个,但它不起作用,实际上阻止了其他项目的打印:

let definitions = sense["definitions"] as? [[String:Any]],
!definitions.isEmpty, case let definition = definitions[0]

非常感谢任何帮助!

0 个答案:

没有答案