json数据绑定到java类
以下是rest客户机上json格式的响应主体json数据绑定到java类,java,json,spring,Java,Json,Spring,以下是rest客户机上json格式的响应主体 [ { "total": 4 }, { "id": "93985", "v": "HDB Hougang", "a": "540 Hougang Avenue 8. (S)530540", "t": "1", "pid": "3522", "aid": "84527", "lid": "93985", "cat": "99", "x": "103.891085
[
{
"total": 4
},
{
"id": "93985",
"v": "HDB Hougang",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "1",
"pid": "3522",
"aid": "84527",
"lid": "93985",
"cat": "99",
"x": "103.891085",
"y": "1.377791",
"bld": "1",
"ad": "0",
"tbiz": "1",
"hsp": 1
},
{
"id": "250286",
"v": "SCDF Bomb Shelter @ 540 Hougang Avenue 8",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "1",
"pid": "49896",
"aid": "84527",
"lid": "250286",
"cat": "1073",
"x": "103.891085",
"y": "1.377791",
"bld": "0",
"ad": "0",
"tbiz": "1",
"hsp": 1
},
{
"id": "386730",
"v": "Everhealth Medical Centre Pte Ltd",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "2",
"pid": "3522",
"aid": "84527",
"lid": "93985",
"cat": "508",
"cid": "189715",
"b": "1",
"tel": "63850815",
"x": "103.891085",
"y": "1.377791",
"bld": "0",
"ad": "0",
"pcn": "HDB Hougang",
"hsp": 1
},
{
"id": "370197",
"v": "Glory Centre Community Services Association",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "2",
"pid": "3522",
"aid": "84527",
"lid": "93985",
"cat": "133",
"cid": "179210",
"b": "1",
"tel": "63863039",
"x": "103.891085",
"y": "1.377791",
"bld": "0",
"ad": "0",
"pcn": "HDB Hougang",
"hsp": 1
}
]
对应的Java类
public class StreetDirectoryDetail implements Serializable {
private int total;
List<StreetDirectoryInfo> info =new ArrayList<StreetDirectoryInfo>();
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public class StreetDirectoryInfo{
private String id;
private String v;
private String a;
private String t;
private String pid;
private String lid;
private String cat;
private double x;
private double y;
private String ad;
private String bld;
private String tbiz;
private String hsp;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
//...the other getter and setter.//
}
}
公共类StreetDirectoryDetail实现可序列化{
私人整数合计;
列表信息=新的ArrayList();
公共int getTotal(){
返回总数;
}
公共无效集合总计(整数总计){
这个.总计=总计;
}
公共类StreetDirectoryInfo{
私有字符串id;
私有字符串v;
私人字符串a;
私有字符串t;
私有字符串pid;
私用线盖;
私人串猫;
私人双x;
私人双y;
私人字符串广告;
私人字符串bld;
私有字符串tbiz;
私有字符串hsp;
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
//…另一个接球手和二传手//
}
}
打电话
String url="<some valid url>";
restTemplate.getMessageConverters().add(
new MappingJackson2HttpMessageConverter());
StreetDirectoryDetail result = restTemplate.getForObject(url,StreetDirectoryDetail.class);
stringurl=”“;
restTemplate.getMessageConverters().add(
新映射Jackson2HttpMessageConverter());
StreetDirectoryDetail结果=restTemplate.getForObject(url,StreetDirectoryDetail.class);
我使用spring的restTemplate将json转换为java等效类。
我尝试使用上面的java类,没有得到“result”,而是直接转到response.close(),没有抛出错误。我怀疑可能是因为StreetDirectoryDetail类的结构不适合从json到java类的数据绑定
希望得到建议,谢谢您的json字符串和类似乎不匹配 您的json字符串
[
{
"total": 4
},
{
"id": "93985",
"v": "HDB Hougang",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "1",
"pid": "3522",
"aid": "84527",
"lid": "93985",
"cat": "99",
"x": "103.891085",
"y": "1.377791",
"bld": "1",
"ad": "0",
"tbiz": "1",
"hsp": 1
},
{
"id": "250286",
"v": "SCDF Bomb Shelter @ 540 Hougang Avenue 8",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "1",
"pid": "49896",
"aid": "84527",
"lid": "250286",
"cat": "1073",
"x": "103.891085",
"y": "1.377791",
"bld": "0",
"ad": "0",
"tbiz": "1",
"hsp": 1
},
{
"id": "386730",
"v": "Everhealth Medical Centre Pte Ltd",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "2",
"pid": "3522",
"aid": "84527",
"lid": "93985",
"cat": "508",
"cid": "189715",
"b": "1",
"tel": "63850815",
"x": "103.891085",
"y": "1.377791",
"bld": "0",
"ad": "0",
"pcn": "HDB Hougang",
"hsp": 1
},
{
"id": "370197",
"v": "Glory Centre Community Services Association",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "2",
"pid": "3522",
"aid": "84527",
"lid": "93985",
"cat": "133",
"cid": "179210",
"b": "1",
"tel": "63863039",
"x": "103.891085",
"y": "1.377791",
"bld": "0",
"ad": "0",
"pcn": "HDB Hougang",
"hsp": 1
}
]
在你们班应该是这样的
[
{
"total": 4,
"info" :
[
{
"id": "93985",
"v": "HDB Hougang",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "1",
"pid": "3522",
"aid": "84527",
"lid": "93985",
"cat": "99",
"x": "103.891085",
"y": "1.377791",
"bld": "1",
"ad": "0",
"tbiz": "1",
"hsp": 1
},
{
"id": "250286",
"v": "SCDF Bomb Shelter @ 540 Hougang Avenue 8",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "1",
"pid": "49896",
"aid": "84527",
"lid": "250286",
"cat": "1073",
"x": "103.891085",
"y": "1.377791",
"bld": "0",
"ad": "0",
"tbiz": "1",
"hsp": 1
},
{
"id": "386730",
"v": "Everhealth Medical Centre Pte Ltd",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "2",
"pid": "3522",
"aid": "84527",
"lid": "93985",
"cat": "508",
"cid": "189715",
"b": "1",
"tel": "63850815",
"x": "103.891085",
"y": "1.377791",
"bld": "0",
"ad": "0",
"pcn": "HDB Hougang",
"hsp": 1
},
{
"id": "370197",
"v": "Glory Centre Community Services Association",
"a": "540 Hougang Avenue 8. (S)530540",
"t": "2",
"pid": "3522",
"aid": "84527",
"lid": "93985",
"cat": "133",
"cid": "179210",
"b": "1",
"tel": "63863039",
"x": "103.891085",
"y": "1.377791",
"bld": "0",
"ad": "0",
"pcn": "HDB Hougang",
"hsp": 1
}
]
}
]
您拥有的JSON是一个JSON数组。您正试图反序列化为一个Java对象,该对象对应于一个JSON对象。Ker p pag,您的示例类和JSON并没有任何区别。