如何使用带有嵌套JSON结构和未知密钥的Swift Codable

时间:2019-05-09 16:00:52

标签: json swift nested codable

我有一个嵌套的json结构。我知道顶层密钥(对象),但是每次获取可能存在或可能不存在。对象中的每个键(以及嵌套在其中的键)都是未知的。它们是动态的。

我尝试了几个小时才使用Codable来代替使用JSONSerialization。在我完全失去希望之前,我想看看是否有人对此有解决方案。

以下是我的JSON的示例:

var jsonString =
"""
{
    "someNumbers": {
        "22": 6,
        "22626": 0
    },
    "someNestedAny": {
        "61": {
            "browser": 2
        },
        "8310": {
            "desktop": 2
        }
    },
    "someNestedArray": {
        "49": ["Chrome"],
        "50": ["Mac OS X"],
        "51": ["Mac desktop"],
        "52": ["browser"],
        "53": ["Chrome"]
    }
}
""" 

每个顶级对象(someNumbers,someNestedAny和someNestedArray)中的键值对都是动态的。这些对象中的键/值也是动态的……等等。每个顶级对象也是可选的。

我尝试了很多事情,但是看起来是最有前途的(两者都不起作用)

  1. 
    struct TopLevel: Decodable {
        var someNumbers: SomeNumbers?
        var someNestedAny: SomeNestedAny?
        var someNestedArray: SomeObjectFromNestedAny?
    }
    
    struct SomeNumbers: Decodable {
        var key: String
        var value: Int
    }
    
    struct SomeNestedAny: Decodable {
        var key: String
        var value: SomeObjectFromNestedAny
    }
    
    struct SomeObjectFromNestedAny: Decodable {
        var key: String
        var value: Int
    }
    
    struct SomeNestedArray: Decodable {
        var key: String
        var value: [String]
    }
    
    let data = jsonString.data(using: .utf8)!
    do {
        let result = try JSONDecoder().decode(TopLevel.self, from: data)
        print(result)
    } catch {
        print(error)
    }
    

输出:keyNotFound(CodingKeys(stringValue: "key", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "someNumbers", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"key\", intValue: nil) (\"key\").", underlyingError: nil))

  1. struct TopLevel: Decodable {
        var someNumbers: SomeNumbers?
        var someNestedAny: SomeNestedAny?
        var someNestedArray: SomeObjectFromNestedAny?
    }
    
    struct SomeNumbers: Decodable {
    
        public var numbersObject: [String: NumberKeys]
    
        public struct NumberKeys: Decodable {
            public let key: String
            public let value: Int
        }
    
        private struct NumberCodingKeys: CodingKey {
            var stringValue: String
    
            init?(stringValue: String) {
                self.stringValue = stringValue
            }
    
            var intValue: Int?
    
            init?(intValue: Int) {
                return nil
            }
        }
    
        public init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: NumberCodingKeys.self)
            self.numbersObject = [String: NumberKeys]()
            for key in container.allKeys {
                let value = try container.decode(NumberKeys.self, forKey: NumberCodingKeys(stringValue: key.stringValue)!)
                self.numbersObject[key.stringValue] = value
            }
    
        }
    }
    
    struct SomeNestedAny: Decodable {
        // was going to do the same thing as above - but it didn't work
        var key: String
        var value: SomeObjectFromNestedAny
    }
    
    struct SomeObjectFromNestedAny: Decodable {
        // was going to do the same thing as above - but it didn't work
        var key: String
        var value: Int
    }
    
    struct SomeNestedArray: Decodable {
        // was going to do the same thing as above - but it didn't work
        var key: String
        var value: [String]
    }
    
    let data = jsonString.data(using: .utf8)!
    do {
        let result = try JSONDecoder().decode(TopLevel.self, from: data)
        print(result)
    } catch {
        print(error)
    }
    

输出:typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "someNumbers", intValue: nil), NumberCodingKeys(stringValue: "22626", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a number instead.", underlyingError: nil))

这是我现在正在工作的东西,但是真的很累:

let data = jsonString.data(using: .utf8)
let json = try JSONSerialization.jsonObject(with: data!) as! [String:Any]
if let numbers = json["someNumbers"] as? [String:Any] {
    for (key, value) in numbers {
        print("key: \(key), value: \(value)")
    }
}
if let anys = json["someNestedAny"] as? [String: Any] {
    //print("tkey: \(anys)")
    for (key, value) in anys {
        //print("tkey2: \(key), tvalue: \(value)")
        if let value = value as? [String: Any] {
            let anyData = try JSONSerialization.data(withJSONObject: value, options: .prettyPrinted)
            let anyJson = try JSONSerialization.jsonObject(with: anyData) as! [String: Any]
            for (key2, value2) in anyJson {
                print("key: \(key), object-key: \(key2), object-value: \(value2)")
            }
        }
    }
}
if let arrays = json["someNestedArray"] as? [String: Any] {
    for (key, value) in arrays {
        print("key: \(key), value: \(value)")
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

很难确定这是否可以解决您的确切用例,因为我不确定数据的目的是什么,以及您打算如何使用它,但是以下两种解决方案都可以正确地将JSON解码为更多可用的Swift对象。

最简单的方法是完全按照您提供的数据结构对其进行建模。例如,看起来someNumbers是由String键控的可选字典,具有Int值:[String: Int]?

struct TopLevel: Decodable {
  var someNumbers: [String: Int]?
  var someNestedAny: [String: [String: Int]]?
  var someNestedArray: [String: [String]]?
}

为了在传递对象时具有更高的可读性,您可以添加一些类型别名,并且该别名变为

typealias SomeNumbers = [String: Int]
typealias SomeNestedAny = [String: [String: Int]]
typealias SomeNestedArray = [String: [String]]

struct TopLevel: Decodable {
  var someNumbers: SomeNumbers?
  var someNestedAny: SomeNestedAny?
  var someNestedArray: SomeNestedArray?
}

要获得有用的东西,您需要调用类似的东西

topLevel.someNumbers?["22"]                 // 6
topLevel.someNestedAny?["8310"]             // ["desktop": 2]
topLevel.someNestedAny?["8310"]?["desktop"] // 2
topLevel.someNestedArray?["52"]             // ["browser"]
topLevel.someNestedArray?["52"]?[0]         // "browser"

或者根据您的需求,遍历事物可能更有意义

topLevel.someNestedAny?
  .forEach { item in
    print("|- \(item.key)")
    item.value.forEach { any in
      print("|  |- \(any.key)")
      print("|  |  |- \(any.value)")
    }
}

// |- 61
// |  |- browser
// |  |  |- 2
// |- 8310
// |  |- desktop
// |  |  |- 2