首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于将JSON推送到本地领域的适当类结构

用于将JSON推送到本地领域的适当类结构
EN

Stack Overflow用户
提问于 2019-04-06 01:49:04
回答 1查看 44关注 0票数 0

好的,所以基本上我要做的是从我创建的api中提取运动鞋数据,该api保存了各种运动鞋的数据。json结构看起来像这样。

代码语言:javascript
复制
  sneakers {
        bramds {
             nike
                 nikeshoe1
                 nikeshoe2
                 ......
            jordan
                 jordanshoe1
                 jordanshoe2
                 ......
}
}

我想在swift中创建一个继承可解码类的类,这样我就可以解析和反序列化json,并将其传递给realm。我看了这本指南,但我仍然有点卡在建模部分。目前我所拥有的是这样的东西

代码语言:javascript
复制
import Foundation
import RealmSwift


struct Sneaker: Decodable {
    var brands : [Brands]

}

struct Brands: Decodable {
    var brandName: String
    var shoe: [Shoe]

}

struct Shoe:Decodable {

    var productlink: String
    var productlinkhref: String
    var name: String
    var price: String
    var releasedate: String
    var colorway: String
    var brand: String
    var designer: String
    var technology: String
    var maincolor: String
    var silhouette: String
    var nickname: String
    var category: String
    var imagesrc: String

}

我只是想知道我是否在正确的轨道上,使这些模型基于我的JSON结构可解码。除此之外,将可解码对象推送到realm的正确语法是什么,它是相同的语法吗?我已经包含了我的JSON的一个示例,并且在任何帮助被提及的情况下

代码语言:javascript
复制
{
"sneakers": {
  "brands" : {
    "Air Jordan": [
      {
            "webscraperorder": "1554084909-97",
            "webscraperstarturl": "https://www.goat.com/sneakers",
            "productlink": "$200AIR JORDAN 6 RETRO 'INFRARED' 2019",
            "productlinkhref": "https://www.goat.com/sneakers/air-jordan-6-retro-black-infrared-384664-060",
            "name": "Air Jordan 6 Retro 'Infrared' 2019",
            "price": "Buy New - $200",
            "description": "The 2019 edition of the Air Jordan 6 Retro ‘Infrared’ is true to the original colorway, which Michael Jordan wore when he captured his first NBA title. Dressed primarily in black nubuck with a reflective 3M layer underneath, the mid-top features Infrared accents on the midsole, heel tab and lace lock. Nike Air branding adorns the heel and sockliner, an OG detail last seen on the 2000 retro.",
            "releasedate": "2019-02-16",
            "colorway": "Black/Infrared 23-Black",
            "brand": "Air Jordan",
            "designer": "Tinker Hatfield",
            "technology": "Air",
            "maincolor": "Black",
            "silhouette": "Air Jordan 6",
            "nickname": "Infrared",
            "category": "lifestyle",
            "imagesrc": "https://image.goat.com/crop/1250/attachments/product_template_additional_pictures/images/018/675/318/original/464372_01.jpg.jpeg"
        },
        {
            "web-scraper-order": "1554084911-110",
            "webscraperstarturl": "https://www.goat.com/sneakers",
            "productlink": "NewMar 30$160AIR JORDAN 1 RETRO HIGH OG 'PHANTOM'",
            "productlinkhref": "https://www.goat.com/sneakers/air-jordan-1-retro-high-og-black-phantom-555088-160",
            "name": "Air Jordan 1 Retro High OG 'Phantom'",
            "price": "Buy New - $160",
            "description": "Instead of the usual two-tone color blocking, the Air Jordan 1 Retro High OG ‘Phantom’ makes use of contrast stitching in black and red to distinguish the high-top’s clean lines. The shoe’s all-leather upper is finished in off-white Sail, accented by a padded collar and underlayer Swoosh in University Red. True to its OG designation, the design is finished with Nike Air branding on the woven tongue tag and insole.",
            "releasedate": "2019-03-30",
            "colorway": "Sail/Black-Phantom-University Red",
            "brand": "Air Jordan",
            "designer": "Peter Moore",
            "technology": "Air",
            "maincolor": "White",
            "silhouette": "Air Jordan 1",
            "nickname": "Phantom",
            "category": "lifestyle",
            "imagesrc": "https://image.goat.com/crop/1250/attachments/product_template_additional_pictures/images/020/095/781/original/411931_06.jpg.jpeg"
        }

如果我忽略了一些细节,请让我知道。

EN

回答 1

Stack Overflow用户

发布于 2019-04-06 02:17:27

看起来你走对了路,但是你看过苹果的例子了吗?

https://developer.apple.com/documentation/foundation/archives_and_serialization/using_json_with_custom_types

我发现这个可下载的游乐场非常有用。

此外,由于您可以设计JSON,因此您可以自由地使JSON镜像您想要的Swift数据结构。因此,我会从这一点向后工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55540882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档