1
我已經通過大量教程瞭解如何解析JSON對象。從Web服務MA JSON對象響應的結構是這樣的從php web服務解析json對象響應
{"posts":[
{"gpoint":{"latitude":"18.966364","longitude":"72.811317"}},
{"gpoint":{"latitude":"19.07023","longitude":"72.82917"}},
{"gpoint":{"latitude":"19.094889","longitude":"72.840157"}},
{"gpoint":{"latitude":"19.056601","longitude":"72.901955"}}]
}
,這是我的代碼:
HttpResponse response= client.execute(post);
content = response.getEntity().getContent();
String result = stringConversion(content);
jObject = new JSONObject(result);
JSONArray gpoint = jObject.getJSONArray("posts");
for (int i = 0; i<gpoint.length();i++)
{
double geoLat=gpoint.getJSONObject(i).optDouble("latitude");
double geoLong=gpoint.getJSONObject(i).optDouble("longitude");
// double geoLat = gPointObject.getDouble("latitude");
// double geoLong = gPointObject.getDouble("longitude");
Log.i(Tag," "+ geoLat);
Log.i(Tag," "+ geoLong);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
我不能夠解析這個響應,每當我解析它,它不給任何解析異常,但經緯度的值是NaN
請告訴我,我哪裏錯了?提前致謝。你錯過了一個嵌套層
JSONObject gp;
for (int i = 0; i<gpoint.length();i++)
{
gp = gpoint.getJSONObject(i);
double geoLat = gp.getJSONObject("gpoint").optDouble("latitude");
double geoLong = gp.getJSONObject("gpoint").optDouble("latitude");
// double geoLat = gPointObject.getDouble("latitude");
// double geoLong = gPointObject.getDouble("longitude");
Log.i(Tag," "+ geoLat);
Log.i(Tag," "+ geoLong);
}
:
thnks一噸,,,,,這是一個很愚蠢的錯誤:( – SAPHIRE 2011-05-30 09:02:50
很高興提供幫助。它始終在發生:你坐得如此接近問題,你沒有注意到你的錯誤。 – Femi 2011-05-30 09:03:49