-5

谁能告诉我如何解析这个 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"
         }
      ]
   }
}
4

1 回答 1

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{

}
于 2017-07-07T15:34:46.527 回答