0

我正在使用 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"
      }
   }
}
4

2 回答 2

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;
    }
}

然后你需要MetaData类。

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;
    }
}

最后,您可以像这样读取经过验证的 jsonJawboneRoot对象。

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"
        }
    }
}
于 2015-06-20T12:16:54.303 回答
0

问题和答案中存在明显的误解。Riddhish Chaudhari将从 Jawbone API 获得的结果是来自由 Retrofit 和 GSON 库制作的 Jawbone 服务器的 JSON 处理结果。尝试阅读我的问题“调用 JAWBONE API 的 JSON 结果中的问题”的答案,我发现这是一个有价值的建议。

于 2015-07-22T10:23:37.893 回答