Handling incrementing JSON name using Swift

后端 未结 2 1591
既然无缘
既然无缘 2021-01-17 03:05

I have a JSON object with incrementing names to parse and I want to store the output into an object with a name field and a list of pet field. I normally use JSONDecoder as

相关标签:
2条回答
  • 2021-01-17 03:22

    Codable has provisions for dynamic keys. If you absolutely can't change the structure of the JSON you're getting, you could implement a decoder for it like this:

    struct VetShop: Decodable {
        let shopName: String
        let pets: [String]
    
        struct VetKeys: 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: VetKeys.self)
            var pets = [String]()
            var shopName = ""
            for key in container.allKeys {
                let str = try container.decode(String.self, forKey: key)
                if key.stringValue.hasPrefix("pet") {
                    pets.append(str)
                } else {
                    shopName = str
                }
            }
            self.shopName = shopName
            self.pets = pets
        }
    }
    
    0 讨论(0)
  • 2021-01-17 03:35

    You can try this to parse your data as Dictionary. In this way, you can get all keys of the dictionary.

        let url = URL(string: "YOUR_URL_HERE")
        URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
            guard let data = data, error == nil else { return }
            do {
                let dics = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! Dictionary<String, Any>
                let keys = [String](dics.keys)
                print(keys) // You have the key list
                print(dics[keys[0]]) // this will print the first value
             } catch let error as NSError {
                print(error)
            }
        }).resume() 
    

    I hope you can figure out what you need to do.

    0 讨论(0)
提交回复
热议问题