【问题标题】:How to Create Gson Object model from Jawbone response如何从 Jawbone 响应创建 Gson 对象模型
【发布时间】:2015-06-20 06:35:12
【问题描述】:

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

【问题讨论】:

    标签: java json parsing gson jawbone


    【解决方案1】:

    首先,您的 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;
        }
    }
    

    最后,您可以像这样将经过验证的 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"
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      问题和答案存在明显的误解。 Riddhish Chaudhari 将从 Jawbone API 获得的结果是由 Retrofit 和 GSON 库对来自 Jawbone 服务器的 JSON 进行处理的结果。 尝试阅读我的问题的答案"Problems in JSON results calling JAWBONE API" 我发现这是一个很有价值的建议。

      【讨论】:

        猜你喜欢
        • 2022-07-30
        • 2018-11-27
        • 2011-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-02
        • 2023-03-06
        • 1970-01-01
        相关资源
        最近更新 更多