I have a get request with the response of json like bellow:
{"version": 0.6,
"generator": "Overpass API",
"osm3s": {
"timestamp_osm_base": "",
"copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
},
"elements": [
{
"type": "node",
"id": 25941318,
"lat": 35.7006285,
"lon": 51.3909900
},
{
"type": "node",
"id": 26839944,
"lat": 35.7006369,
"lon": 51.3913739
},
{
"type": "node",
"id": 1333387625,
"lat": 35.7012370,
"lon": 51.3913564
}
]
}
I need to convert this Json to java object, this is my model class:
package com.findItntersection.model;
public class Intersection {
String type;
private long id;
private double lon;
private double lat;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
@Override
public String toString() {
return "elements [ type=" + type + ",id=" + id + ",lon=" + lon +
".lat" + lat + "]";
}}
and my service method which sends get request correctly:
public void sendGet(String city, String street1, String street2)
throws ClientProtocolException, IOException, URISyntaxException {
Gson gson = new Gson();
URIBuilder builder = new URIBuilder();
builder.setScheme("http")
.setHost("192.168.0.67")
.setPath("/api/interpreter")
.setParameter(
"data",
"[out:json];area[name~\""
+ city
+ "\"]->.b;way(area.b)[highway][name~\""
+ street1
+ "\"];node(w)->.n1;way(area.b)[highway][name~\""
+ street2 + "\"];node(w)->.n2;node.n1.n2;out;");
URI uri = builder.build();
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
System.out.println(response1);
HttpEntity entity1 = response1.getEntity();
String json = EntityUtils.toString(entity1,"UTF-8");
Intersection fromJson = gson.fromJson(json, Intersection.class);
System.out.println(fromJson);
EntityUtils.consume(entity1);
} finally {
response1.close();
}
}
but the fromJson is like this: [ type=null,id=0,lon=0.0.lat0.0]
the result from get request json:
{
"version": 0.6,
"generator": "Overpass API",
"osm3s": {
"timestamp_osm_base": "",
"copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
},
"elements": [
{
"type": "node",
"id": 29004231,
"lat": 35.7212341,
"lon": 51.3888708
}
]
}
Can anybody help me please?