0

I am using Jawbone SDK https://github.com/Jawbone/UPPlatform_Android_SDK

and would liken to parse this response into Gson https://jawbone.com/up/developer/endpoints/trends

How can I create Gson object for this response?

{
   “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"
      }
   }
}
Sevle
  • 3,109
  • 2
  • 19
  • 31
Riddhish.Chaudhari
  • 833
  • 1
  • 8
  • 24

2 Answers2

0

First of all your json is not valid. You must fix that before putting it to Gson. (Double quotes, commas etc.)

First you need a Root class to read all.

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

Then you need Meta and Data classes.

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

Then you need class for your 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;
    }
}

And at last you can read your validated json to JawboneRoot object like this.

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

And since i already make your json valid, here is your validated version of 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"
        }
    }
}
bhdrkn
  • 6,244
  • 5
  • 35
  • 42
0

There is a clear misunderstanding in the question and in the answer. What Riddhish Chaudhari will get from the Jawbone API is the result of the processing of the JSON coming from Jawbone server made by the Retrofit and GSON libraries. Try to read the answer to my question "Problems in JSON results calling JAWBONE API" I found it a valuable suggestion.

Community
  • 1
  • 1
infoartenovo
  • 93
  • 1
  • 5