我试图在以下结构中设置var VendorIDDeviceID的rawValue。
struct vendor: Codable {
var VendorIDDeviceID: String
enum CodingKeys: String, CodingKey {
case VendorIDDeviceID = "Vendor\(vendorID)Device\(deviceID)"
}
}
但是我需要能够在编码过程中填写vendorID和deviceID的变量。我看了一下互联网的深度,找不到任何与执行此操作相关的事情。我找到的最接近的是:
let vendorID = "1002"
let deviceID = "67df"
let enumCase = vendor.VendorIDDeviceID(rawValue: "Vendor\(vendorID)Device\(deviceID)")
但是没有看到我如何在编码过程中实现这一点。任何帮助将不胜感激。
答案 0 :(得分:0)
这是我尝试从https://pastebin.com/raw/LALX2PvV解析JSON的方式
由于MACPro
对象中的键是事先未知的,因此它使用动态键并将解析的结果存储在字典中。
struct BundleData: Codable {
let cfBundleName, cfBundleIdentifier,
cfBundleInfoDictionaryVersion, osBundleRequired: String
let cfBundleVersion, cfBundleExecutable,
cfBundleGetInfoString, cfBundleSignature: String
let cfBundlePackageType: String
let ioKitPersonalities: IOKitPersonalities
let buildMachineOSBuild, lsMinimumSystemVersion,
cfBundleDevelopmentRegion, nsHumanReadableCopyright: String
let cfBundleShortVersionString: String
enum CodingKeys: String, CodingKey {
case cfBundleName = "CFBundleName"
case cfBundleIdentifier = "CFBundleIdentifier"
case cfBundleInfoDictionaryVersion = "CFBundleInfoDictionaryVersion"
case osBundleRequired = "OSBundleRequired"
case cfBundleVersion = "CFBundleVersion"
case cfBundleExecutable = "CFBundleExecutable"
case cfBundleGetInfoString = "CFBundleGetInfoString"
case cfBundleSignature = "CFBundleSignature"
case cfBundlePackageType = "CFBundlePackageType"
case ioKitPersonalities = "IOKitPersonalities"
case buildMachineOSBuild = "BuildMachineOSBuild"
case lsMinimumSystemVersion = "LSMinimumSystemVersion"
case cfBundleDevelopmentRegion = "CFBundleDevelopmentRegion"
case nsHumanReadableCopyright = "NSHumanReadableCopyright"
case cfBundleShortVersionString = "CFBundleShortVersionString"
}
}
struct IOKitPersonalities: Codable {
let agpm: Agpm
enum CodingKeys: String, CodingKey {
case agpm = "AGPM"
}
}
struct Agpm: Codable {
let cfBundleIdentifier, ioProviderClass, ioClass: String
let machines: Machines
let ioNameMatch: String
enum CodingKeys: String, CodingKey {
case cfBundleIdentifier = "CFBundleIdentifier"
case ioProviderClass = "IOProviderClass"
case ioClass = "IOClass"
case machines = "Machines"
case ioNameMatch = "IONameMatch"
}
}
struct Machines: Codable {
let macPro41, macPro51: MACPro
enum CodingKeys: String, CodingKey {
case macPro41 = "MacPro4,1"
case macPro51 = "MacPro5,1"
}
}
struct MACPro: Codable {
var defaultControlID: Int
let devices: [String: DeviceInfo]
enum CodingKeys: String, CodingKey {
case defaultControlID = "default-control-id"
}
struct DeviceKey: CodingKey {
var stringValue: String
var intValue: Int?
init?(stringValue: String) {
self.stringValue = stringValue
}
init?(intValue: Int) {
self.stringValue = "\(intValue)";
self.intValue = intValue
}
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: DeviceKey.self)
self.defaultControlID = 0
var devices = [String: DeviceInfo]()
for key in container.allKeys {
if let device = try? container.decode(DeviceInfo.self, forKey: key) {
devices[key.stringValue] = device
} else if let controlId = try? container.decode(Int.self, forKey: key) {
self.defaultControlID = controlId
}
}
self.devices = devices
}
}
struct DeviceInfo: Codable {
let heuristic: Heuristic
let logControl, controlID: Int
enum CodingKeys: String, CodingKey {
case heuristic = "Heuristic"
case logControl = "LogControl"
case controlID = "control-id"
}
}
struct Heuristic: Codable {
let thresholdLow: [Int]
let idleInterval, sensorSampleRate, targetCount, sensorOption: Int
let thresholdHigh: [Int]
let id: Int
enum CodingKeys: String, CodingKey {
case thresholdLow = "Threshold_Low"
case idleInterval = "IdleInterval"
case sensorSampleRate = "SensorSampleRate"
case targetCount = "TargetCount"
case sensorOption = "SensorOption"
case thresholdHigh = "Threshold_High"
case id = "ID"
}
}
let data = json.data(using: .utf8)!
do {
let bundleData = try JSONDecoder().decode(BundleData.self, from: data)
print(bundleData)
}
catch {
print(error)
}
答案 1 :(得分:0)
此处说明的CustomCodingKeys也可用于编码:https://stackoverflow.com/a/50715560/6324550
redirect_to_url