我可以使用Swift获取此JSON:
let estado = json["estado"] as? String
if estado == "1" {
print("Estado OK")
}
使用以下代码:
If(field.isEmpty)
但是,我想访问转弯属性。我尝试了许多组合而没有成功。
如果可能的话,我希望像Android一样拥有一些东西,直接将Turno作为Turno对象。
你们能帮助我吗?
答案 0 :(得分:0)
turno
是一个包含String
个键和String
值的字典,因此不需要向下投射。
此示例打印键_id
和Descripcion
的值(如果可用)。
if let turno = json["turno"] as? [String:String] {
if let identifier = turno["_id"] { print(identifier) }
if let description = turno["Descripcion"] { print(description) }
}
答案 1 :(得分:0)
尝试以下
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
答案 2 :(得分:0)
我不知道任何可以自动从字典中创建对象的Swift库。
但是自己制作并不难,只有一些样板。
一个例子可能是这样......
根据字典键制作对象及其初始值设定项:
struct Turno {
let _id:String
let _idEmpresa:String
let _idCentro:String
let description:String
let turnoActual:String
let turnoSiguiente:String
let version:String
init(fromDictionary dictionary: [String:String]) {
_id = dictionary["_id"] ?? ""
_idEmpresa = dictionary["_idEmpresa"] ?? ""
_idCentro = dictionary["_idCentro"] ?? ""
description = dictionary["Descripcion"] ?? ""
turnoActual = dictionary["TurnoActual"] ?? ""
turnoSiguiente = dictionary["TurnoSiguiente"] ?? ""
version = dictionary["Version"] ?? ""
}
}
然后将字典传递给结构构造函数:
if let content = json["turno"] as? [String:String] {
let turno = Turno(fromDictionary: content)
print(turno._id)
print(turno._idEmpresa)
print(turno._idCentro)
print(turno.description)
print(turno.turnoActual)
print(turno.turnoSiguiente)
print(turno.version)
}