简体   繁体   English

JSONArray中未命名的JSONArray

[英]Unnamed JSONArray inside JSONArray

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. 通常,我将遍历JSONArray并使用jsonObject.getInt(“ whatever”)访问对象,但是在这种情况下,我不能这样做。 Not sure how to achieve this. 不知道如何实现这一目标。

Thanks in advance =) 在此先感谢=)

Create JSONArray object from your JSON string and then iterate using getJSONArray(int index) method. 从您的JSON字符串创建JSONArray对象,然后使用getJSONArray(int index)方法进行迭代。 Finally use getDouble(int index) and getLong(int index) to retrieve values from inner arrays. 最后,使用getDouble(int index)和getLong(int index)从内部数组检索值。

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


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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