我能够使用 Swift 获取此 JSON:
{
"estado": "1",
"turno": {
"_id": "1",
"_idEmpresa": "1",
"_idCentro": "1",
"Descripcion": "LOCAL",
"TurnoActual": "40",
"TurnoSiguiente": "53",
"Version": "2185"
}
}
使用以下代码:
let estado = json["estado"] as? String
if estado == "1" {
print("Estado OK")
}
但是,我想访问回合属性。我尝试了很多组合但没有成功。
如果可能的话,我想要像在 Android 上那样直接将 Turno 作为对象 Turno 获取。
你们能帮我解决这个问题吗?
最佳答案
尝试以下
let dictResult = json["turno"] as! NSDictionary
let id = dictResult["_id"] as! String
let idEmpresa = dictResult["_idEmpresa"] as! String
// OR you can directly get data, if you have json as a NSDictionary
let id = json.objectForKey("turno")?.objectForKey("_id") as! String
let idEmpresa = json.objectForKey("turno")?.objectForKey("_idEmpresa") as! String
关于ios - 检索 JSON 对象 Swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33803851/