首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解码JSON Swift 4-嵌套对象/数组

解码JSON Swift 4-嵌套对象/数组
EN

Stack Overflow用户
提问于 2018-07-19 22:45:36
回答 1查看 139关注 0票数 4

有人能看到我错过了什么吗?我不能破译任何过去的结果。任何东西都不会在结果下打印出来。我查看了其他几篇与JSON/Swift相关的文章,但仍然不明白我做错了什么。这是我的JSON:

代码语言:javascript
复制
{
  "results": [
    {
      "user.ldap.principal": "OHWIL3336IPM101",
      "common.os_version": "11.4",
      "common.wifi_mac_address": "100caef1001d",
      "common.status": "ACTIVE",
      "common.creation_date": "2018-17-05T16:42:49.000Z",
      "ios.iPhone UDID": "a8a7a2e52359353dfbacf026a4fada9ew1cb4c10",
      "user.ldap.user_attributes.custom1": [
        "3336"
      ],
      "common.SerialNumber": "F9FWEF74GHMN",
      "common.uuid": "01cd1ed3-b3af-48c0-8499-654c0a9ab996"
    }
  ],
  "totalCount": 1,
  "resultCount": 1,
  "searchTimeMillis": 1,
  "currentServerTimeMilliseconds": 1531558334959,
  "hasMore": false
}

这是我目前的情况。

代码语言:javascript
复制
struct DeviceData: Codable {
    let results: [Result]
    let totalCount, resultCount, searchTimeMillis, currentServerTimeMilliseconds: Int
    let hasMore: Bool
}

struct Result: Codable {
    let commonOSVersion, commonStatus, commonImei, commonCreationDate: String?
    let iosDeviceName, commonUUID, userLDAPPrincipal, commonWifiMACAddress: String?
    let iosIPhoneUDID: String?
    let userLDAPUserAttributesCustom1: [String]?
    let commonSerialNumber: String?
    let userLDAPGroupsName: [String]?
    let iosIPhoneICCID: String?

    enum CodingKeys: String, CodingKey {
        case commonOSVersion = "common.os_version"
        case commonStatus = "common.status"
        case commonImei = "common.imei"
        case commonCreationDate = "common.creation_date"
        case iosDeviceName = "ios.DeviceName"
        case commonUUID = "common.uuid"
        case userLDAPPrincipal = "user.ldap.principal"
        case commonWifiMACAddress = "common.wifi_mac_address"
        case iosIPhoneUDID = "ios.iPhone UDID"
        case userLDAPUserAttributesCustom1 = "user.ldap.user_attributes.custom1"
        case commonSerialNumber = "common.SerialNumber"
        case userLDAPGroupsName = "user.ldap.groups.name"
        case iosIPhoneICCID = "ios.iPhone ICCID"
    }
 }

试图解码:

代码语言:javascript
复制
   let decoder = JSONDecoder()
   guard let data = data else {return}
   do {
    let json = try decoder.decode(DeviceData.self, from: data)
    dump(json)
    print(json.commonImei) //Does not print - Does not auto-populate - Error Here
    } 
    catch let jsonError {
    print("JSON Failed to Decode: ", jsonError)
    }

错误:

代码语言:javascript
复制
 Value of type 'DeviceData' has no member 'commonImei'

json将完整地打印到控制台,但是如果我试图打印结果(结果)中的任何字段,则这些值不会自动填充,并且会收到一个错误。我是不是漏掉了解码的东西?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-19 22:49:57

你需要do-catch

代码语言:javascript
复制
do {
     let decoder = JSONDecoder()
     let json = try decoder.decode(DeviceData.self, from: data)
     dump(json)
     print(json.results[0].commonImei)
}
catch {
    print(error)
}

//

json表示struct DeviceData的一个对象,它不直接包含commonImei,但是它有一个数组结果,其中所有的元素都包含该键

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51432689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档