【问题标题】:json data binding to java classjson数据绑定到java类
【发布时间】:2014-08-06 04:10:40
【问题描述】:

以下是 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 类,我没有得到“结果”,而是直接转到 response.close() 而不会抛出错误。我怀疑可能是由于 StreetDirectoryDe​​tail 类的结构不适合从 json 到 java 类的数据绑定。

希望建议,谢谢

【问题讨论】:

  • 您拥有的 JSON 是一个 JSON 数组。您正在尝试反序列化为与 JSON 对象相对应的 Java 对象。

标签: java json spring


【解决方案1】:

您的 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
   }
  ]
 }
]

【讨论】:

  • Ker p pag ,您的示例类与 json 没有任何区别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 2017-02-08
相关资源
最近更新 更多