Using a web service I'm requesting histogram data. The data is a set of arrays inside an array:
[[1375056000000,23.284713745117],[1375142400000,3.809531211853],
[1375228800000,9.6309003829956],[1375315200000,2.7411839962006]]
I want to be able to store the key pair values in a Hash Map. Usually I would iterate through a JSONArray and access objects using jsonObject.getInt("whatever"), but in this case I can't. Not sure how to achieve this.
Thanks in advance =)
Create JSONArray object from your JSON string and then iterate using getJSONArray(int index) method. Finally use getDouble(int index) and getLong(int index) to retrieve values from inner arrays.
in your example:
JSONArray a1 = new JSONArray("[[1375056000000,23.284713745117],[1375142400000,3.809531211853]]");
for (int i=0; i<a1.length(); i++) {
JSONArray a2 = a1.getJSONArray(i);
long v1 = a2.getLong(0);
double v2 = a2.getDouble(1);
}
String json = "[[1375056000000,23.284713745117],[1375142400000,3.809531211853],[1375228800000,9.6309003829956],[1375315200000,2.7411839962006]]";
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++){
jsonArray.getJSONArray(i).getLong(0); //do something with the key
jsonArray.getJSONArray(i).getDouble(1); //do something with the value
}
String yourJsonArray;
JSONArray root = new JSONArray(yourJsonArray);
int rootSize = root.length();
for (int i = 0, i < rootSize; i++) {
JSONArray inner = root. getJSONArray(i);
long firstChildren = inner.getLong(0);
double secondChildren = inner.getDouble(1);
}
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.