ios - 检索 JSON 对象 Swift 2

标签 ios json swift swift2

我能够使用 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/

相关文章:

c# - Newtonsoft.Json.Linq.JArray' 键入 'System.Collections.Generic.IEnumerable

swift - 如何在披露指示器上启用用户交互,但不在 UITableViewCell 上启用

ios - 如何在 didFinishLaunchingWithOptions 方法中通过单击通用链接检查应用程序是否已打开

ios - iPad - 使用 CLLocationManager 的问题

ios - 家庭应用程序图标在我的 iphone 6 设备上没有正确显示。全黑

objective-c - 无法在 View 中设置标签

java - 在java中将json字符串转换为JSONArray?

ios - 将 NSData(Json 数据)转换为 nsarray : iOS

xcode - 如何阻止 reloaddata 刷新页面?

ios - "icon dimensions (60 * 60) don' t 满足大小要求”将我的应用程序提交到应用程序商店