如何将json转换为java对象?
我有一个get请求,响应类似于下面的json:如何将json转换为java对象?,java,json,gson,Java,Json,Gson,我有一个get请求,响应类似于下面的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
{"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
}
]
}
我需要将这个Json转换成java对象,
这是我的模型课:
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 + "]";
}}
以及我正确发送get请求的服务方法:
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();
}
}
但是fromJson是这样的:
[type=null,id=0,lon=0.0.lat0.0]
获取请求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
}
]
}
有人能帮我吗?这是因为您的模型类不正确。 类交集只是映射json中的元素。 您应该有一个映射所有json元素的模型类,其中“元素”作为定义自己元素的另一个类
这个例子应该会有所帮助 这是因为您的回答不是
交叉点。它是一个包含交点数组的对象
String json = EntityUtils.toString(entity1,"UTF-8");
JsonParser jp = new JsonParser();
JsonObect jo = ( JsonObject ) jp.parse( json );
// JsonArray of intersection JsonElement's
JsonArray ija = ( JsonArray ) jo.get( "elements" );
// Iterator of intersection JsonElements
Iterator<JsonElement> iji = ija.iterator();
List< Intersection> il = new ArrayList< Intersection >();
while( iji.hasNext() ) {
JsonElement je = iji.next();
// convert to Intersection
Intersection i = gson.fromJson( je, Intersection.class );
// add to List
il.add( i );
}
// do whatever you want with your list of intersections.
因此,首先需要将JSON转换为JsObject
,然后获取此数组并将其转换为交叉点列表
String json = EntityUtils.toString(entity1,"UTF-8");
JsonParser jp = new JsonParser();
JsonObect jo = ( JsonObject ) jp.parse( json );
// JsonArray of intersection JsonElement's
JsonArray ija = ( JsonArray ) jo.get( "elements" );
// Iterator of intersection JsonElements
Iterator<JsonElement> iji = ija.iterator();
List< Intersection> il = new ArrayList< Intersection >();
while( iji.hasNext() ) {
JsonElement je = iji.next();
// convert to Intersection
Intersection i = gson.fromJson( je, Intersection.class );
// add to List
il.add( i );
}
// do whatever you want with your list of intersections.
String json=EntityUtils.toString(entity1,“UTF-8”);
JsonParser jp=新的JsonParser();
jsonobectjo=(JsonObject)jp.parse(json);
//交叉点JsonElement的JsonArray
JsonArray ija=(JsonArray)jo.get(“元素”);
//交集元素的迭代器
迭代器iji=ija.Iterator();
列表il=newarraylist();
while(iji.hasNext()){
JsonElement je=iji.next();
//转换为交叉口
交叉点i=gson.fromJson(je,Intersection.class);
//添加到列表中
二.添加(i);
}
//用交叉口列表做任何你想做的事情。
Gson无法解析,因为您的响应不是交叉点
类的表示
它代表着这样的东西:
public class OsmResult implements Serializable{
private double version;
private String generator;
private HashMap<String, String> osm3s;
private ArrayList<Intersection> elements;
...getters & setters
}
public类OsmResult实现可序列化{
私人双版;
专用字符串生成器;
私有HashMap osm3s;
私有数组列表元素;
…接受者和接受者
}
从这里,你可以走两条路
提取元素
并将其解析为交叉点
列表
我认为Gson中没有提取方法,但您可以为此尝试其他Json库。或者,您可以执行简单的字符串操作来提取元素
将其解析为类似上面的类,只需使用getElements()
方法
如果您坚持使用Gson,我建议您这样做,因为这是一种更干净的方法。您是否从get请求中获得了正确的结果?(也就是说,你得到你想要的jsonObject了吗)是的,编辑了这篇文章,结果是正确的,我在我的模型中也尝试过:@Override public String to String(){return“{\n\”version\”:06、\n\“generator\”:“Overpass API\”,\n\“osm3s\”:{\n\“timestamp\u osm\u base\”:“,\n\“copyright\”:“本文档中包含的数据来自www.openstreetmap.org。这些数据在ODbL下可用。\“\n}”,“+”\n\“元素”:[\n\n{\n\“类型”:“+type+”,\n\“id\”:“+id+”,\n\“lat\”:“+lat+”,\n\“lon\”:“+lon+”};}但不起作用