大家好我正在阅读包含JAVA(Android)
的JSON对象的页面以下是该页面包含的内容:
{
"destination_addresses" : [ "Safi, Maroc" ],
"origin_addresses" : [ "Avenue Hassan II, Safi, Maroc" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1,0 km",
"value" : 966
},
"duration" : {
"text" : "2 minutes",
"value" : 129
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
我知道如何从执行此操作的页面中读取
JSONObject jArray = new JSONObject(result);
String elements = jArray.getString("rows");
元素字符串包含:
[{"elements" : [{"distance" : {"text" : "1,0 km","value" : 966},"duration" : {"text" : "2 minutes","value" : 129},"status" : "OK"}]}]
但我试图获得距离值,即“ 966 ”
Thx guys
答案 0 :(得分:2)
您可能需要查看以下链接:
但是对于您的特定问题,您想要调用类似的内容:
int value = jArray.getJSONObject("rows").getJSONObject("elements").getJSONArray("distance").getInteger("value");
答案 1 :(得分:2)
所以你试图从数组中访问一个条目。请参阅教程或示例。这是基本的东西: http://www.mkyong.com/java/json-simple-example-read-and-write-json/
您需要实际使用JSONArray(子类JSONObject)
答案 2 :(得分:1)
试试这个..
JSONObject jObj = new JSONObject(result);
JSONArray rows = jObj.getJSONArray("rows");
for(int i = 0; i < rows.length; i++){
JSONObject obj = rows.getJSONObject(i);
JSONArray elements = jObj.getJSONArray("elements");
for(int j = 0; j < elements.length; j++){
JSONObject Jobj = elements.getJSONObject(j);
JSONObject distance = Jobj.getJSONObject("distance");
int distance_value = distance.getInteger("value");
JSONObject duration = Jobj.getJSONObject("duration");
int duration_value = duration.getInteger("value");
}
}