从JSON对象中获取JSON数组(已解决)

huangapple 未分类评论43阅读模式
英文:

Getting a JSON Array from JSON Object (SOLVED)

问题

第一次自己处理JSON数据,即使非常简单。

这是我正在处理的JSON数据:

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

我试图做的是从该JSON对象中提取三个数组中的一个,但我收到了一个JSONException: Not a primitive array: class org.json.JSONArray。以下是我用于提取与“heart”相关的值数组的代码部分:

JSONObject obj = new JSONObject(obtainJSONObject());
JSONArray arr = new JSONArray(obj.getJSONArray("heart")); // 这是发生错误的地方

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

我尝试过按照这个解决方案的回答进行操作,但无法理解太多:https://stackoverflow.com/questions/32624166/how-to-get-json-array-within-json-object/32624372

我不确定是否与JSON数据的格式有关。我在https://jsonformatter.curiousconcept.com/上检查过它是否有效。任何帮助或见解将不胜感激!

英文:

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: https://stackoverflow.com/questions/32624166/how-to-get-json-array-within-json-object/32624372

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!

huangapple
  • 本文由 发表于 2020年4月8日 10:48:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/61092569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定