让结果=尝试?NSJSONSerialization.JSONObjectWithData(数据,选项:.MutableContainers)
如何从swift2.3中的给定JSON
获取值
({
FirstName:"sample"
LastName:"Data"
},
{
FirstName:"sample1"
LastName:"Data1"
})
如何从名字中获取值并添加到数组swift 2.3和Xcode8
答案 0 :(得分:0)
好的,尝试使用 json 是json字典
let firstname = json["FirstName"] as! String
答案 1 :(得分:0)
以下代码将您给定的json转换为字符串,然后使用NSJSONSerialization来解析您的json,最后打印FirstName和Lastname的值
let jsonString = "[{\"FirstName\":\"sample\",\"LastName\":\"Data\"},{\"FirstName\":\"sample1\",\"LastName\":\"Data1\"}]"
if let serializedJsonArray = try? NSJSONSerialization.JSONObjectWithData(jsonString.dataUsingEncoding(NSUTF8StringEncoding)!, options: .MutableContainers),
let parsedJsonArray = serializedJsonArray as? [[String:AnyObject]]{
for jsonArray in parsedJsonArray {
if let firstName = jsonArray["FirstName"] as? String{
print(firstName)
}
if let lastName = jsonArray["LastName"] as? String{
print(lastName)
}
}
}
希望这有帮助。