首页 文章

可解码字典[字符串:任意] [重复]

提问于
浏览
-1

这个问题在这里已有答案:

我正在尝试通过Swifts Decodable类使用JSON Rest API . API包含一个字典,其字符串为字符串,值有时为 String ,有时为 Bool . 我've created structs for all subtrees of the JSON, but can'弄清楚如何使用字符串 OR Bool的字典 .

这是图像参数(下面)是 [String : String] OR [String : Bool] .

struct Item:Decodable {
    var id: String
    var name: String
    var price: String
    var priceIcon: String
    var priceIconLink: String
    var images: [String : String]
    var rarity: String
    var type: String
    var readableType: String
}

试过这个,但它不符合Decodable .

var images: [String : Any]

JSON响应的示例

"images": {
                "icon": "https://image.fnbr.co/emote/5ae8a0edf3d31bd9cac5b80d/icon.png",
                "png": "https://image.fnbr.co/emote/5ae8a0edf3d31bd9cac5b80d/png.png",
                "gallery": "https://image.fnbr.co/emote/5ae8a0edf3d31bd9cac5b80d/gallery.jpg",
                "featured": false
            }

1 回答

  • -1

    解决了!我在解析之前对JSON字符串进行了一些预处理 . 用空字符串(“”)替换所有false .

    解:

    dataString.replacingOccurrences(of: ":false}", with: ":\"\"}", options: .literal, range: nil)
    

相关问题