我使用的是Jawbone SDK https://github.com/Jawbone/UPPlatform_Android_SDK
并且会将此回复解析为Gson https://jawbone.com/up/developer/endpoints/trends
如何为此响应创建Gson对象?
{
“meta”:
{
“user_xid”: “6xl39CsoVp2KirfHwVq_Fx”,
“message”: “OK”,
“code”: 200,
"time": 1386711801
},
“data”:
{
"earliest": 20120525,
"data":
[[
20131105,
{
"weight": 94.6
"height": 1.8034
"gender": false
"age": 29.5835616438
"bmr": 2002.0974546
"body_fat": 25
"goal_body_weight_intent": 1,
"goal_body_weight": 50.8,
"m_steps": 2184
"m_calories": 129.136649132
"m_total_calories": 2131.23410373
"m_active_time": 1093
"m_workout_time": 0
"m_distance": 1795
"e_calories": 530
"e_carbs": 64.75
"e_cholesterol": 50
"e_protein": 16.77
"e_calcium": 140
"e_unsat_fat": 15.313
"e_sat_fat": 6
"e_sodium": 504
"e_sugar": 19
"e_fiber": 0
"s_bedtime": -370
"s_asleep_time": 298
"s_awake": 1820
"s_awake_time": 24530
"s_awakenings": 2
"s_light": 9828
"s_sound": 13252
"s_duration": 23080
"s_quality": 69
"n_bedtime": null
"n_asleep_time": null
"n_awake": null
"n_awake_time": null
"n_awakenings": null
"n_light": null
"n_sound": null
"n_duration": null
"n_quality": null
}
],[
... more data ...
]],
"links":
{
"next": "/nudge/api/v.1.1/users/6xl39CsoVp2KirfHwVq_Fx/trends/?end_date=20131104&range=w&range_duration=5&bucket_size=d"
}
}
}
答案 0 :(得分:0)
首先你的json无效。在将其放入Gson
之前,您必须先修复此问题。 (双引号,逗号等)
首先,您需要Root类来阅读所有内容。
public class JawboneRoot {
private Meta meta;
private Data data;
public Meta getMeta() {
return meta;
}
public void setMeta(Meta meta) {
this.meta = meta;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
然后您需要Meta
和Data
类。
public class Meta {
private String user_xid;
private String message;
private Integer code;
private Integer time;
public String getUser_xid() {
return user_xid;
}
public void setUser_xid(String user_xid) {
this.user_xid = user_xid;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Integer getTime() {
return time;
}
public void setTime(Integer time) {
this.time = time;
}
}
public class Data {
private Integer earliest;
private Map<Integer, SubData> data;
private Map<String, String> links;
public Integer getEarliest() {
return earliest;
}
public void setEarliest(Integer earliest) {
this.earliest = earliest;
}
public Map<Integer, SubData> getData() {
return data;
}
public void setData(Map<Integer, SubData> data) {
this.data = data;
}
public Map<String, String> getLinks() {
return links;
}
public void setLinks(Map<String, String> links) {
this.links = links;
}
}
然后您需要为SubData
上课。
public class SubData {
private Double weight;
private Double height;
private Integer body_fat;
// Add others like this
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public Integer getBody_fat() {
return body_fat;
}
public void setBody_fat(Integer body_fat) {
this.body_fat = body_fat;
}
}
最后,您可以将经过验证的json读取到JawboneRoot
这样的对象。
public class JsonApp {
private static final String TEST_JSON = "validated--json";
public static void main(String[] args) throws Exception {
final Gson gson = new GsonBuilder().create();
final JawboneRoot jawboneClass = gson.fromJson(TEST_JSON, JawboneRoot.class);
System.out.println(jawboneClass);
}
}
由于我已经使你的json有效,这里是你经过验证的json版本。
{
"meta": {
"user_xid": "6xl39CsoVp2KirfHwVq_Fx",
"message": "OK",
"code": 200,
"time": 1386711801
},
"data": {
"earliest": 20120525,
"data": [
[
20131105,
{
"weight": 94.6,
"height": 1.8034,
"gender": false,
"age": 29.5835616438,
"bmr": 2002.0974546,
"body_fat": 25,
"goal_body_weight_intent": 1,
"goal_body_weight": 50.8,
"m_steps": 2184,
"m_calories": 129.136649132,
"m_total_calories": 2131.23410373,
"m_active_time": 1093,
"m_workout_time": 0,
"m_distance": 1795,
"e_calories": 530,
"e_carbs": 64.75,
"e_cholesterol": 50,
"e_protein": 16.77,
"e_calcium": 140,
"e_unsat_fat": 15.313,
"e_sat_fat": 6,
"e_sodium": 504,
"e_sugar": 19,
"e_fiber": 0,
"s_bedtime": -370,
"s_asleep_time": 298,
"s_awake": 1820,
"s_awake_time": 24530,
"s_awakenings": 2,
"s_light": 9828,
"s_sound": 13252,
"s_duration": 23080,
"s_quality": 69,
"n_bedtime": null,
"n_asleep_time": null,
"n_awake": null,
"n_awake_time": null,
"n_awakenings": null,
"n_light": null,
"n_sound": null,
"n_duration": null,
"n_quality": null
}
]
],
"links": {
"next": "/nudge/api/v.1.1/users/6xl39CsoVp2KirfHwVq_Fx/trends/?end_date=20131104&range=w&range_duration=5&bucket_size=d"
}
}
}
答案 1 :(得分:0)
在问题和答案中存在明显的误解。从Jawbone API获得的Riddhish Chaudhari是由Retrofit和GSON库处理来自Jawbone服务器的JSON的结果。 尝试阅读我的问题的答案"Problems in JSON results calling JAWBONE API"我发现这是一个有价值的建议。