Внутренняя структура JSON конфликтует с декодируемыми методами - PullRequest
1 голос
/ 06 апреля 2019

У меня есть структура 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"
          }
    ]
  }
}

Я пытаюсь декодировать эту структуру json, используя декодируемую swift 4, и это доставляет мне немного хлопот.

Текущие структуры, которые у меня есть для декодирования значений, таковы:

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
    }
}

Но я продолжаю получать ошибку в отношении структуры

Ожидается декодирование массива, но вместо этого найден словарь

Однако я знаю, что словари не соответствуют декодируемым, может кто-нибудь увидеть, что не так, основываясь на моей структуре JSON.

1 Ответ

1 голос
/ 06 апреля 2019

Вы можете попробовать это

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
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...