我如何在此json中添加“位置”?
它给我“ null”,这是json代码的简单内容....................... ................................................... ................................................... ................................................... ................................................... .....................................
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 31.8004863,
"lng" : 35.155975
},
"viewport" : {
"northeast" : {
"lat" : 31.80183612989272,
"lng" : 35.15732482989272
},
"southwest" : {
"lat" : 31.79913647010727,
"lng" : 35.15462517010727
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "74f5b809b62fcf35766480a616de5001c470247e",
"name" : "בורגרס בר | Burgers Bar",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 4048,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/103176736009300448033/photos\"\u003eינון בר ישועה\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAcoAMqy_VbKFHSy1npn6QMLAoFxaM7Jlc-hS2PtiSopKPizvIEsErjCB6VIW3HrWeiXfOX2npcp8uXHdQ4tALfu6B9w_gbPqJFfknqEfHjKqMY2voYDljGZ8xhXyC3F7TEhD5fejHnVYdp4dk-hnwR1qiGhTlEWS0jlKa3vFdz3FhX4MAUZAUiA",
"width" : 3036
}
],
"place_id" : "ChIJy7TbP-TWAhUR5rzxuO1Vi9g",
"plus_code" : {
"compound_code" : "R524+59 מבשרת ציון",
"global_code" : "8G3QR524+59"
},
"rating" : 4.2,
"reference" : "ChIJy7TbP-TWAhUR5rzxuO1Vi9g",
"scope" : "GOOGLE",
"types" : [ "restaurant", "point_of_interest", "food", "establishment" ],
"vicinity" : "האורן 47, מבשרת ציון"
},
我的包含json的类
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
public class MapModel implements Serializable {
private double lat;
private double lng;
private String vicinity;
private String name;
private String id;
private String icon;
JSONObject json;
JSONObject loc;
{
try {
loc = json.getJSONObject("geometry").getJSONObject("location");
lat = loc.getDouble("lat");
lng = loc.getDouble("lng");
} catch (JSONException e) {
e.printStackTrace();
}
}
public MapModel(String name, String vicinity, double lat, double lng, String icon) {
this.vicinity = vicinity;
this.name = name;
this.lat = lat;
this.lng = lng;
this.icon = icon;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public double getLat() {
return lat;
}
public void setLat(double location) {
this.lat = location;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getVicinity() {
return vicinity;
}
public void setVicinity(String formatted_address) {
this.vicinity = formatted_address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:-1)
您将要使用此方法添加到JSON
JSONObject
放置(字符串名称,对象值)
如果您要获取空值,那么您实际上是在放置一个空对象
但是,您已经有位置了,所以不清楚您的最终目标是什么...
在这里,您可以看到我正在解析您现有的JSON
String s = // not clear where this is coming from
MapModel m = new MapModel(); // building this object
try {
JSONObject json = new JSONObject(s);
JSONObject loc = json.getJSONObject("geometry").getJSONObject("location"):
int lat = loc.getDouble("lat");
int lng = loc.getDouble("lng");
// And then assign the data
m.setLat(lat); m.setLng(lng);
} catch (Exception e) {
e.printStackTrace();
}
// load m into some UI elements
请勿将此代码放入您的MapModel中。将它放在
处的JSON字符串之后的任何位置