5

我已经开始转换一些 JSON 解析代码以使用新的 AppleDecodable协议,并且遇到了一个在 Apple 测试期间感觉太基本以至于无法错过的阻止程序,所以我想知道我是否在做一些愚蠢的事情。简而言之,我正在尝试解析这样的 JSON 图,因为我只是在解码,所以我认为符合 Decodable 应该就足够了,但从错误看来,我需要符合 Codable (Decodable & Encodable) 以获得所需解码效果:

{"keyString": {"nestedKey1" : "value1", "nestedKey1" : "value1" } }

它适用于这种情况:

{"keyString": [{"nestedKey1" : "value1", "nestedKey1" : "value1" } ]}

as 是嵌套对象的数组,但不是单个对象。

这是一个 Swift 错误还是我做错了什么?

这是一个可以证明问题的示例游乐场。如果 Animal 类符合Decodable它不会解析数组案例,但如果我将 Animal 设置为符合,Codable那么它确实有效。我不希望会出现这种情况,因为我在这里只解码 JSON。

import Foundation

//class Animal: Codable {    
class Animal: Decodable {
    var fileURLPath: String = ""
    var age: Double = 0
    var height: Double = 0
    var weight: Double = 0

    private enum CodingKeys: String, CodingKey {
        case fileURLPath = "path"
        case age
        case height
        case weight
    }

    required init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        fileURLPath = try values.decode(String.self, forKey: .fileURLPath)
        age = try values.decode(TimeInterval.self, forKey: .age)
        height = try values.decode(Double.self, forKey: .height)
        weight = try values.decode(Double.self, forKey: .weight)
    }
}

let innerObjectJSON = """
{
    "path": "tiger_pic.png",
    "age": 9,
    "height": 1.23,
    "weight": 130
}
"""

let innerObjectData = innerObjectJSON.data(using: String.Encoding.utf8)

let jsonDataNestedObject = """
    { "en" : \(innerObjectJSON)
    }
    """.data(using: String.Encoding.utf8)

let jsonDataNestedArray = """
    { "en" : [\(innerObjectJSON), \(innerObjectJSON), \(innerObjectJSON) ]
    }
    """.data(using: String.Encoding.utf8)

print("Nested Array of Objects:")

do {
    let result = try JSONDecoder().decode([String: [Animal]].self, from: jsonDataNestedArray!)
    result["en"]!.forEach ({ print($0.fileURLPath) }) // This one works
} catch { print(error) }

print("\n\n Single Object:")
do {
    let result = try JSONDecoder().decode(Animal.self, from: innerObjectData!)
        print(result.fileURLPath)
} catch { print(error) }

print("\n\nNested Object:")
do {
    let result = try JSONDecoder().decode([String: Animal].self, from:jsonDataNestedObject!)
        print(result["en"]!.fileURLPath) // I would also expect this to work but I get the error: "fatal error: Dictionary<String, Animal> does not conform to Decodable because Animal does not conform to Decodable.: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.45.6/src/swift/stdlib/public/core/Codable.swift, line 3420"
} catch { print(error) }
4

1 回答 1

0

如果您不想等到下一个版本,您可以使用struct

import Cocoa

struct Animal: Codable {
    var fileURLPath: String
    var age: Double
    var height: Double
    var weight: Double

    private enum CodingKeys: String, CodingKey {
        case fileURLPath = "path"
        case age, height, weight
    }
}

let innerObjectJSON = """
{
"path": "tiger_pic.png",
"age": 9,
"height": 1.23,
"weight": 130
}
"""

let innerObjectData = innerObjectJSON.data(using: String.Encoding.utf8)

let jsonDataNestedObject = """
    { "en" : \(innerObjectJSON)
    }
    """.data(using: String.Encoding.utf8)


print("\n\nNested Object:")
do {
    let result = try JSONDecoder().decode([String: Animal].self, from:jsonDataNestedObject!)
    print(result["en"]!.fileURLPath) 
} catch { print(error) }

这给了我

Nested Object:
tiger_pic.png
["en": __lldb_expr_190.Animal(fileURLPath: "tiger_pic.png", age: 9.0, height: 1.23, weight: 130.0)]

它很容易解码,但它不使用TimeInterval这只是 Double 的别名。

于 2018-05-21T16:57:09.423 回答