小编Mad*_*lin的帖子

如何在 Swift 4 中使用根元素作为数组正确解析 JSON?

我从 API 得到以下响应:

[
    {
        "stores": [
            {
                "store": {
                    "Name": "A store name",
                    "City": "NY"
                }
            },
            {
                "store": {
                    "Name": "A second store name",
                    "City": "ny2"
                }
            }
        ]
    }, 

    {

        "products": [
            {
                "product": {
                    "name": "a product",
                    "price": "1"
                }
            },
            {
                "product": {
                    "name": "a second product",
                    "price": "2"
                }
            }
        ]

    }

]
Run Code Online (Sandbox Code Playgroud)

对于两个 JSON 对象(商店和产品),我创建了以下结构:

struct shops_response: Codable {

    var stores: [stores]
}

struct products_response: Codable {

    var products: [products]
}


struct stores: …
Run Code Online (Sandbox Code Playgroud)

json swift4

1
推荐指数
1
解决办法
1713
查看次数

标签 统计

json ×1

swift4 ×1