有谁能告诉我如何解析这个JSON响应?我需要提取"姓氏"来自服务。
{
"Entity":{
"ID":1,
"UserTypeID":1,
"Code":"lPEq",
"Services":[
{
"ID":118,
"Code":"1",
"Parent_ID":null,
"Name":"Alex",
"lastName":"John"
},
{
"ID":119,
"Code":"2",
"Parent_ID":null,
"Name":"Christy",
"lastName":"Noel"
}
]
}
}
答案 0 :(得分:0)
这是你如何做到的。不要忘记处理解包
let str = "{ \"Entity\":{ \"ID\":1, \"UserTypeID\":1, \"Code\":\"lPEq\", \"Services\":[ { \"ID\":118, \"Code\":\"1\", \"Parent_ID\":null, \"Name\":\"Alex\", \"lastName\":\"John\" }, { \"ID\":119, \"Code\":\"2\", \"Parent_ID\":null, \"Name\":\"Christy\", \"lastName\":\"Noel\" } ] } }"
let data = str.data(using: .utf8)
do{
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any]
let entityDic = json?["Entity"] as? [String: Any]
let servicesDic = entityDic?["Services"] as? [Any]
let firstPerson = servicesDic?[0] as? [String: Any]
dump(firstPerson?["lastName"])
}catch let error{
}