【发布时间】:2019-01-11 04:59:45
【问题描述】:
我正在从开关数组中解析“switch_name”,但在解析时我得到 nil 值
{
"status": "true",
"result": {
"hubs": [
{
"hub_id": "1",
"user_id": "35",
"switch": [
{
"id": "4",
"hub_id": "1",
"switch_name": "Test2",
"user_id": "35",
"serial_no": "445112",
"topic_sense": "rer",
"device_room": "25",
"switch_type": "LIGHTS",
"types_of_relay_switch": "S"
}
],
"relay": []
}
],
"switchwithouhub": []
}
}
我是如何解析的:-
let sName = jsonDict.value(forKeyPath: "result.hubs.switch.switch_name") as? [String]
我在解析 switch_name 时得到 nil 值。 请帮助并建议我如何解析 JSON
【问题讨论】:
-
result.hubs是一个数组,而不是字典。 -
我不知道更多关于 keyPath 的使用,但是 hubs 是数组你不需要定义索引或任何东西吗?
-
jsonviewer.stack.hu 见 Json 以正确的格式检查是哪种数据类型并定义数组或字典或单个对象。你肯定会发现不对的地方