如何在Swift中解析空字典JSON?

时间:2017-03-17 19:54:56

标签: json swift parsing

我使用swift解析JSON。我来到一本空白的字典。我如何将其解析为自定义对象?

**我在看Image **

},
        "name": "Al bake",
        "image": {},
        "website": "",
        "category": {
            "id": "d59d2b1c-ca37-4c76-bb93-1c0ba967ee84",
            "name": "Test Category",
            "slug": "test-category",
            "is_active": true
        },

1 个答案:

答案 0 :(得分:0)

如果您需要image的值,它应该是字典(空或包含键和值),您需要知道包含它的类型。

例如,我们假设以下代码包含在字典中:

"name": "Al bake",
"image": {},
"website": "",
"category": {
    "id": "d59d2b1c-ca37-4c76-bb93-1c0ba967ee84",
    "name": "Test Category",
    "slug": "test-category",
    "is_active": true

你会做这样的事情:

if let image = dictionary["image"] as? Dictionary<String, Any> {
    print(image.description)
} else {
    print("Image does not contain a value")
}