gpt4 book ai didi

json - 内部 JSON 结构与 Decodable 方法冲突

转载 作者:行者123 更新时间:2023-11-28 05:44:09 25 4
gpt4 key购买 nike

我有一个看起来像这样的 json 结构。

    {
"sneakers" : {
"brands" : {
"Adidas" : [ {
"brand" : "adidas",
"category" : "lifestyle",
"colorway" : "Cream White/Cream White/Core White",
"description" : "First released on April 29, 2017, the Yeezy Boost 350 V2 ‘Cream White’ combines a cream Primeknit upper with tonal cream SPLY 350 branding, and a translucent white midsole housing full-length Boost. Released again in October 2018, this retro helped fulfill Kanye West’s oft-repeated ‘YEEZYs for everyone’ Twitter mantra, as adidas organized the biggest drop in Yeezy history by promising pre-sale to anyone who signed up on the website. Similar to the first release, the ‘Triple White’ 2018 model features a Primeknit upper, a Boost midsole and custom adidas and Yeezy co-branding on the insole.",
"designer" : "Kanye West",
"imagesrc" : "https://image.goat.com/crop/1250/attachments/product_template_additional_pictures/images/014/822/695/original/116662_03.jpg.jpeg",
"maincolor" : "White",
"name" : "Yeezy Boost 350 V2 'Cream White / Triple White'",
"nickname" : "Cream White / Triple White",
"price" : "Buy New - $220",
"productlink" : "$190YEEZY BOOST 350 V2 'CREAM WHITE / TRIPLE WHITE'",
"productlinkhref" : "https://www.goat.com/sneakers/yeezy-boost-350-v2-cream-white-cp9366",
"releasedate" : "2017-04-29",
"silhouette" : "Yeezy Boost 350",
"technology" : "Boost",
"web-scraper-order" : "1554084922-147",
"webscraperstarturl" : "https://www.goat.com/sneakers"
}, {
"brand" : "adidas",
"category" : "running",
"colorway" : "Footwear White/Footwear White/Footwear White",
"description" : "The adidas Ultra Boost 4.0 'Triple White' launched in November 2016. The fourth edition of adidas’ performance runner features a blank-canvas aesthetic, featuring crisp white on the shoe’s Primeknit upper, laces and supportive midfoot cage. A bit of shine comes courtesy of the silver Ultra Boost branding on the heel stabilizer, while the black finish on the Continental Rubber outsole offers a lone shot of contrast.",
"designer" : "Ben Herath",
"imagesrc" : "https://image.goat.com/crop/1250/attachments/product_template_additional_pictures/images/010/222/174/original/264258_08.jpg.jpeg",
"maincolor" : "White",
"name" : "UltraBoost 4.0 'Triple White'",
"nickname" : "Triple White",
"price" : "Buy New - $180 $110",
"productlink" : "$90ULTRABOOST 4.0 'TRIPLE WHITE'",
"productlinkhref" : "https://www.goat.com/sneakers/ultra-boost-4-0-triple-white-bb6168",
"releasedate" : "2017-11-16",
"silhouette" : "UltraBoost",
"technology" : "Boost",
"web-scraper-order" : "1554084884-8",
"webscraperstarturl" : "https://www.goat.com/sneakers"
}
]
}
}

我正在尝试使用 swift 4 decodable 解码这个 json 结构,这给我带来了一些麻烦。

我用于解码值的当前结构是这样的

import Foundation
import RealmSwift


struct Sneaker: Decodable {
let sneakers : Sneakers

}

struct Sneakers: Decodable {
let brands: Shoe
}



struct Shoe: Decodable {
let adidas: [SneakerInfo]
let nike: [SneakerInfo]
let airjordan: [SneakerInfo]
let vans: [SneakerInfo]
enum CodingKeys: String, CodingKey {
case adidas = "Adidas"
case nike = "Nike"
case airjordan = "Air Jordan"
case vans = "Vans"
}
}


struct SneakerInfo: Decodable {
let brand, category, colorway, description: String
let designer: String
let imagesrc: String
let maincolor, name, nickname, price: String
let productlink: String
let productlinkhref: String
let releasedate, silhouette, technology, webscraperorder: String
let webscraperstarturl: String
enum CodingKeys: String, CodingKey {
case brand, category, colorway, description, designer, imagesrc, maincolor, name, nickname, price, productlink, productlinkhref, releasedate, silhouette, technology,webscraperorder,webscraperstarturl
}
}

但我一直收到关于结构的错误

Expected to decode Array but found a dictionary instead

但是我知道字典不符合 decodable 任何人都可以根据我的 JSON 结构看出问题所在。

最佳答案

你可以试试这个

struct Sneaker: Codable {
let sneakers: Sneakers
}

struct Sneakers: Codable {
let brands: Shoe
}

struct Shoe: Codable {
let adidas: [Adida]

enum CodingKeys: String, CodingKey {
case adidas = "Adidas"
}
}
struct Adida: Codable {
let brand, category, colorway, description: String
let designer: String
let imagesrc: String
let maincolor, name, nickname, price: String
let productlink: String
let productlinkhref: String
let releasedate, silhouette, technology, webScraperOrder: String
let webscraperstarturl: String

enum CodingKeys: String, CodingKey {
case brand, category, colorway, description, designer, imagesrc, maincolor, name, nickname, price, productlink, productlinkhref, releasedate, silhouette, technology
case webScraperOrder = "web-scraper-order"
case webscraperstarturl
}
}

关于json - 内部 JSON 结构与 Decodable 方法冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com