简体   繁体   English

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

[英]Decoding JSON Swift 4 - Nested Objects/Arrays

Can anyone see what I'm missing? 任何人都能看到我错过的东西吗? I can't decode anything past results. 我无法解码过去的结果。 Nothing will print under results. 结果下不会打印任何内容。 I've reviewed several other posts that are relevant to JSON/Swift but still don't understand what I'm doing wrong. 我已经回顾了其他一些与JSON / Swift相关的帖子,但仍然不明白我做错了什么。 This is my JSON: 这是我的JSON:

{
  "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
}

Here is what I have currently. 这是我目前的情况。

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"
    }
 }

Trying to decode: 试图解码:

   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)
    }

Error: 错误:

 Value of type 'DeviceData' has no member 'commonImei'

The json will print to the console in full but if I try to print any fields within Result (results) the values don't auto populate and I receive an error. json将完全打印到控制台,但如果我尝试打印Result(结果)中的任何字段,则值不会自动填充,并且我收到错误。 Am I missing something with decoding? 我错过了解码的东西吗?

You need do-catch 你需要do-catch

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

// //

The json represents an object of the struct DeviceData which doesn't contain commonImei directly , but it has an array results where all it's elemnts contain that key json表示结构DeviceData的一个对象,它不直接包含commonImei ,但是它有一个数组结果,其中所有的元素都包含该键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM