我收到來自foursquare的場地列表,我需要每個場地的名稱,地址和城市。在這裏,我的問題是如果地址或城市是空的,它會給出錯誤,並說「JSON異常:地址沒有值」。我嘗試了不同的方法來修復它,但無法解決問題。我希望你能幫助我。謝謝。帶空字段的JSON對象
JSONObject b = venues.getJSONObject(i);
JSONObject location = b.getJSONObject("location");
String name = "";
String address = "";
String city = "";
if ("".equals(b.get("name"))) {
// Toast.makeText(Activity2.this,"Name of Place: " +name+ " Location: "+country, Toast.LENGTH_SHORT).show();
name = "No place ";
} else {
name = b.getString("name");
}
if ("".equals(location.getString("address"))) {
address = "No address";
} else {
address = location.getString("address");
}
if ("".equals(location.getString("city"))) {
city = "No city";
} else {
city = location.getString("city");
}
name = "Name of the place: " + name;
// + " Address: "+ address + " City: " + city;
list.add(name);
感謝隊友它是好的,知道這個optString功能 –
+1,對我來說非常有用:) – Jorgesys