0

First time having to work with JSON data on my own, even if very simple.

Here is the JSON data I'm working with:

{
    "heart" : [92, 108],
    "temperature" : [85.08, 85.66],
    "conductance" : [4095, 4095]
}

What I'm attempting to do is extract one of the three arrays found within that JSON object, but I'm receiving a JSONException: Not a primitive array: class org.json.JSONArray. Here is a portion of the code that I'm using to extract the array of values associated with "heart":

JSONObject obj = new JSONObject(obtainJSONObject());
JSONArray arr = new JSONArray(obj.getJSONArray("heart")); // This is where the error is occuring

int low = arr.getInt(0);
int high = arr.getInt(1);

I've tried to follow what this solution answered, but can't really make much sense of it: How to Get JSON Array Within JSON Object?

I'm not sure if it has something to do with the way how the JSON data is being formatted? I did check online to see if it was any valid or not at https://jsonformatter.curiousconcept.com/. Any help or insights will be greatly appreciated!

3
  • 1
    iirc you don't need to create a new instance to wrap around the obj.getJSONArray() Commented Apr 8, 2020 at 2:57
  • Came here to say that - OP, in the link you provided they had JSONArray ja_data = jsonObj.getJSONArray("data");, so your code should be JSONArray arr = obj.getJSONArray("heart"); Commented Apr 8, 2020 at 2:58
  • Guess I've been staring at my own screen for too long that I didn't notice it. Thanks for the help! Commented Apr 8, 2020 at 3:02

0

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.