json数据绑定到java类

时间:2014-08-06 04:10:41

标签: java json spring

以下是json

中rest客户端的响应正文
[
  {
    "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.//

    }

}

并致电

String url="<some valid url>";
restTemplate.getMessageConverters().add(
                new MappingJackson2HttpMessageConverter());
StreetDirectoryDetail result =     restTemplate.getForObject(url,StreetDirectoryDetail.class);

我使用spring的restTemplate将json转换为java等效类。 我尝试上面的java类,我没有得到&#34;结果&#34; ,但直接转到response.close()没有抛出错误。我怀疑可能由于StreetDirectoryDe​​tail类的结构不适合从json到java类的数据绑定。

希望建议,谢谢

1 个答案:

答案 0 :(得分:0)

似乎与你的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
   }
  ]
 }
]