我使用swift解析JSON。我来到一本空白的字典。我如何将其解析为自定义对象?
**我在看Image
**
},
"name": "Al bake",
"image": {},
"website": "",
"category": {
"id": "d59d2b1c-ca37-4c76-bb93-1c0ba967ee84",
"name": "Test Category",
"slug": "test-category",
"is_active": true
},
答案 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")
}