简体   繁体   中英

Java JSON return an array as the value

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!

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

I think this should do the job:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM