我有一个看起来像这样的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可解码的方法解码此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
}
}
但是我在结构方面总是遇到错误
打算对Array进行解码,但是找到了一个字典
但是我知道字典不符合可解码的规定,任何人都可以根据我的JSON结构查看问题所在。
答案 0 :(得分:1)
您可以尝试
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
}
}