1

I have a JSON object like

{
    "endtime": 1446188340,
    "interval": 60,
    "metrics": {
        "heartrate": {
            "values": [
                88,
                92,
                88,
                89,
                86,
                84,
                82,
                86,
                97,
                77,
                81,
                87,
                83,
                101,
                96,
                97,
                123,
                123,
                127,
                127,
                127,
                130,
                134,
                133,
                129,
                126,
                121,
                137,
                141,
                149,
                144,
                120,
                104,
                102,
                100,
                107,
                116,
                107,
                98,
                97,
                115,
                107,
                106,
                98
            ]
        }
    },
    "starttime": 1446102000,
    "timezone_history": [
        {
            "offset": -7,
            "start": 1446102000,
            "timezone": "America\/Los_Angeles"
        }
    ]
}

How would I get an array of the heartrate data under "values"?

If I print:

JSONObject a = new JSONObject(obj.getJSONObject("metrics").getJSONObject("heartrate"));

I just get:

{}

And it seems that JSONArray is not the right thing to use either. I just want to get an array of doubles that I can work with. Thank you!

2 Answers 2

2

JSON Rule of Thumb:


  • '[' represents starting of an JSONArray node

  • '{' represents JSONObject.

If your JSON node starts with [, then we should use getJSONArray() method. While if the node starts with {, then we should use getJSONObject() method.

Here is the code to get Double Values

   public static ArrayList<Double> getHeartRates(String jsonString) throws JSONException {
            ArrayList<Double> values = new ArrayList<>();
            // root JSON Object.
            JSONObject jsonObject = new JSONObject(jsonString);

            JSONObject metrics = jsonObject.getJSONObject("metrics");
            JSONObject heartRate = metrics.getJSONObject("heartrate");
            JSONArray valuesArray = heartRate.getJSONArray("values");

            for (int i = 0; i < valuesArray.length(); i++) {                                       
                values.add(valuesArray.getDouble(i));
            }

            return values;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your solution. I was wondering if you could clear something up for me then. I thought a JSON array was an array of JSON objects, so like ["key1": value, "key2": value, "key3":value]. But what I have is a JSON object where the value is an array, like "key1":[1,2,3]. How come you are calling [1,2,3] a JSONArray? Thank you!
0

I think this should do the job:

JSONObject main = new JSONObject(MyJsonString);
JSONArray values = main.getJSONObject("metrics")
                    .getJSONObject("heartrate").getJSONArray("values");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.