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!

  • 1
    iirc you don't need to create a new instance to wrap around the `obj.getJSONArray()` – Animesh Sahu Apr 08 '20 at 02: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");` – Scary Wombat Apr 08 '20 at 02:58
  • Guess I've been staring at my own screen for too long that I didn't notice it. Thanks for the help! – Incapamentum Apr 08 '20 at 03:02

0 Answers0