您好我正在尝试从另一个Json数组中的Json Array获取数据。我试图解析它,但我无法做到。这是我的JSON数据。
{
"aggregate": "volume",
"dimension1": "queries",
"dimension2": "days",
"results": [
{
"id": 1998364102,
"name": "Denso",
"data": {},
"values": [
{
"id": "2015-05-23T00:00:00.000+0000",
"name": "2015-05-23 00:00:00.0",
"value": 5,
"data": {}
},
{
"id": "2015-05-25T00:00:00.000+0000",
"name": "2015-05-25 00:00:00.0",
"value": 3,
"data": {}
},
{
"id": "2015-05-21T00:00:00.000+0000",
"name": "2015-05-21 00:00:00.0",
"value": 9,
"data": {}
},
{
"id": "2015-05-22T00:00:00.000+0000",
"name": "2015-05-22 00:00:00.0",
"value": 0,
"data": {}
},
{
"id": "2015-05-19T00:00:00.000+0000",
"name": "2015-05-19 00:00:00.0",
"value": 7,
"data": {}
},
{
"id": "2015-05-24T00:00:00.000+0000",
"name": "2015-05-24 00:00:00.0",
"value": 8,
"data": {}
},
{
"id": "2015-05-20T00:00:00.000+0000",
"name": "2015-05-20 00:00:00.0",
"value": 15,
"data": {}
},
{
"id": "2015-05-26T00:00:00.000+0000",
"name": "2015-05-26 00:00:00.0",
"value": 11,
"data": {}
}
]
}
]
}
如何从"值中获取数据"阵列?我需要从那里获得价值和名称。我的JAVA代码:
if (jsonObject.has("results")) {
JSONArray jArray = jsonObject.getJSONArray("results");
for (int i = 0; i < jArray.length(); i++) {
JSONArray valueArray = new JSONArray(jArray.getJSONObject(i).getJSONArray("values"));
for (int j = 0; j < valueArray.length(); j++) {
JSONObject name = valueArray.getJSONObject(j);
System.out.println(j+"Value:"+name.getInt("value"));
}
}
}
答案 0 :(得分:0)
if (jsonObject.has("results")) {
JSONArray jArray = jsonObject.getJSONArray("results");
while(jArray.iterator().hasNext()){
JSONObject valueArrayObject = ((JSONObject)jArray.iterator().next());
if(valueArrayObject.containsKey("values")){
JSONArray valueObject = (JSONArray)valueArrayObject.get("values");
while(valueObject.iterator().hasNext()){
JSONObject valueObj = (JSONObject)valueObject.iterator().next();
System.out.println(valueObj.get("id"));
}
}
}
}
迭代结果数组以获取值数组。您必须为任何解析器错误添加异常处理。
答案 1 :(得分:0)
谢谢大家。我得到了答案。此代码有效
if (jsonObject.has("results")) {
JSONArray resultArray = jsonObject.getJSONArray("results");
for (int i=0;i<resultArray.length();i++) {
JSONObject resultObj = resultArray.getJSONObject(i);
if(resultObj.has("values")){
JSONArray valuesArray= resultObj.getJSONArray("values");
for(int j=0; j<valuesArray.length();j++){
JSONObject jObj = valuesArray.getJSONObject(j);
System.out.println(j+" "+"Date: " + jObj.getString("id"));
System.out.println(j+" "+"Volume: " + jObj.getInt("value"));
}
}
}
}