我想在此链接解析JSON文件:http://maps.googleapis.com/maps/api/directions/json?origin=33.7489,-84.3881&destination=33.7514,-84.7478&sensor=false
我想从“steps”数组中解析“html_instructions”字符串:
String googledirectionsapi = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination0 + "&sensor=false";
try {
JSONObject googleObject = Directions.getJSONfromURL(googledirectionsapi);
JSONArray parsedGoogleDirections = googleObject.getJSONArray("routes").getJSONArray("steps");
thing.setText(googledirectionsapi);
if(parsedGoogleDirections.equals("") ){
Toast.makeText(FindDealer.this, "Cannot find suitable route", Toast.LENGTH_LONG).show();
}else{
for (int i = 0; i < parsedGoogleDirections.length(); i++) {
JSONObject instructions = parsedGoogleDirections.getJSONObject(i);
if (i == 0){
String step1 = parsedGoogleDirections.getString("html_instructions");
}
}
}
}catch (JSONException e) {
Log.d("log_tag","JSON parsing error - Google Directions Api:" + e.getMessage());
}
然而,eclipse在给我错误:
.getJSONArray("steps"); //<-- The method getJSONArray(int) in the type JSONArray is not applicable for the arguments (String).
并且还给我错误:
.getString("html_instructions"); //<--he method getString(int) in the type JSONArray is not applicable for the arguments (String)
为什么eclipse会显示这些错误?我以前从未遇到过这个问题。提前感谢你。
答案 0 :(得分:2)
有一个类似的问题:JSON parsing of Google Maps API in Android App
我认为你首先应该使用索引从JsonArray获取一个Object。然后你应该得到腿作为数组。然后你应该从腿数组得到一个对象。然后你可以获得步骤数组。