{
"destination_addresses" : [ "Bombay, Maharashtra, Inde" ],
"origin_addresses" : [ "New Delhi, New Delhi 110001, Inde" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1 457 km",
"value" : 1457222
},
"duration" : {
"text" : "22 heures 8 minutes",
"value" : 79663
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
回答
請使用以下代碼作爲參考。
JSONObject jsonObject = new JSONObject("jsonResponse");
JSONArray jsonArr= jsonObj.getJSONArray("destination_addresses");
String[] arrDestinations=new String[jsonArr.lLength()];
for(int i=0;i<jsonArr.length();i++)
arrDestinations[i]=jsonArr.get(i);
jsonArr= jsonObj.getJSONArray("origin_addresses");
String[] arrOrigins=new String[jsonArr.lLength()];
for(int i=0;i<jsonArr.length();i++)
arrOrigins[i]=jsonArr.get(i);
JSONArray rows = jsonObj.getJSONArray("rows");
for(int i =0; i < rows.length(); i++)
{
JSONObject rowObj = rows.getJSONObject(i);
JSONArray rowElements = rowObj.getJSONArray("elements");
for(int element = 0; element < rowElements.length(); element++) {
// get the elements and parse
}
}
希望這會有所幫助。
謝謝你回覆我已經追蹤了json文檔:) –
如果這對你有幫助,那麼請upvote並接受答案:) –
檢查您的Logcat
以查看JsonObject
中提取的值。
try {
JSONObject jsonObj = new JSONObject(JsonObject.toString());
JSONArray destination_addresses = jsonObj.getJSONArray("destination_addresses");
for (int i = 0; i < destination_addresses.length(); i++) {
Log.e("Values", "destination_addresses = " + destination_addresses.get(i));
}
JSONArray origin_addresses = jsonObj.getJSONArray("origin_addresses");
for (int i = 0; i < origin_addresses.length(); i++) {
Log.e("Values", "origin_addresses = " + origin_addresses.get(i));
}
JSONArray rows = jsonObj.getJSONArray("rows");
for (int i = 0; i < rows.length(); i++) {
JSONObject rowObject = rows.getJSONObject(i);
JSONArray rowElements = rowObject.getJSONArray("elements");
for (int j = 0; j < rowElements.length(); j++) {
JSONObject elementObject = rowElements.getJSONObject(j);
JSONObject distanceObject = elementObject.getJSONObject("distance");
String distanceText = distanceObject.getString("text");
String distanceValue = distanceObject.getString("value");
JSONObject durationObject = elementObject.getJSONObject("duration");
String durationText = distanceObject.getString("text");
String durationValue = distanceObject.getString("value");
Log.e("Values", "distanceText = " + distanceText + "\ndistanceValue = " + distanceValue + "\ndurationText = " + durationText + "\ndurationValue = " + durationValue);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
感謝您的回覆我已追蹤到JSON文檔:) –
歡迎:) @HarshaVardhan –
第一件事我已經將它轉換成json Hirarchy樹,然後我明白解析:)此工具確實有助於http ://basic4ppc.com:51042/JSON/index.html的 –
我寫了一個庫,用於分析和Android中產生JSON: http://github.com/amirdew/JSON
- 1. 如何使用Volley解析JSON請求?
- 2. 如何使用Volley解析JSON結果?
- 3. 如何使用Volley解析JSON?
- 4. 如何使用volley解析json數據
- 5. 如何使用Volley庫解析JSON數組?
- 6. 嵌套的JSON對象解析在android中使用volley
- 7. 解析json代碼到android中的字符串使用volley
- 8. Android Volley JSON解析問題NULL檢查
- 9. `錯誤:org.json.JSONException`爲json解析使用volley
- 10. 使用Volley的Android Studio JSON解析:獲取NullPointerException
- 11. 如何使用org-json庫解析json?
- 12. 如何使用volley解析rss提要?
- 13. 如何使用Volley獲取和解析JSON對象
- 14. 如何使用volley JSON android實現SearchView?
- 15. 如何解析使用android的json url?
- 16. 使用Gson庫解析Android JSON
- 17. Volley org.json.JSONException當解析有效的JSON
- 18. JSON解析使用簡單的JSON庫
- 19. 使用API解析Android JSON
- 20. Android Json使用Gson解析
- 21. 如何解析JSON文件,而無需使用JSON庫和JSON解析器
- 22. android如何解析json
- 23. 如何解析.netcore類庫中的JSON
- 24. 如何使用JSON解析
- 25. 如何使用Json解析?
- 26. 如何解析Android中的json對象?
- 27. 如何解析Android中的JSON
- 28. 如何解析另一個JSONObject內的JSONObject,使用Android Volley進行HTTP連接
- 29. 從JSON使用Volley的Android ImageAdapter
- 30. JSON解析Android中
你寫的代碼只。沒有任何關於你想做什麼的解釋,我怎能甚至試圖幫助你? – phaberest
解析到ListView?看看這個:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ –