【发布时间】:2015-06-29 14:11:26
【问题描述】:
我正在编写一个应用程序来使用他们的 REST API 和 Square 的 Retrofit 库来控制飞利浦 Hue 智能灯。
问题是,当我调用 /lights 时,响应返回时使用每个灯光的 id 属性作为 json 响应中的键(而不是 jsonapi 响应中典型的灯光对象数组,这似乎是 Retrofit 所期望的)。
这是我正在查看的请求/响应
GET /lights
返回
``` {
"1": {
"state": {
"on": true,
"bri": 144,
"hue": 13088,
"sat": 212,
"xy": [0.5128,0.4147],
"ct": 467,
"alert": "none",
"effect": "none",
"colormode": "xy",
"reachable": true
},
"type": "Extended color light",
"name": "Hue Lamp 1",
"modelid": "LCT001",
"swversion": "66009461",
"pointsymbol": {
"1": "none",
"2": "none",
"3": "none",
"4": "none",
"5": "none",
"6": "none",
"7": "none",
"8": "none"
}
},
"2": {
"state": {
"on": false,
"bri": 0,
"hue": 0,
"sat": 0,
"xy": [0,0],
"ct": 0,
"alert": "none",
"effect": "none",
"colormode": "hs",
"reachable": true
},
"type": "Extended color light",
"name": "Hue Lamp 2",
"modelid": "LCT001",
"swversion": "66009461",
"pointsymbol": {
"1": "none",
"2": "none",
"3": "none",
"4": "none",
"5": "none",
"6": "none",
"7": "none",
"8": "none"
}
}
} ```
请注意,它不是返回一个灯光对象数组,而是返回每个灯光对象与其灯光 ID 相关联。
有人知道如何用 Retrofit 解析这个吗?
【问题讨论】:
-
你使用 GSON 或类似的东西吗? AFAIR GSON 无法解析带注释字段的正则表达式,因此看起来最好的方法是使用 Jackson 库进行解析并手动将其转换为数组。
标签: android json rest retrofit philips-hue