把一个字符串的json转化成java对象出现异常
把一个json格式的字符串转化成对应的对象出错,下面的是具体的代码的抛出的异常。希望大神指点一下
下面是把sting类型转化成json类型的工具类,其中formatToList为转换方法
package com.taotao.common.bean; import java.io.IOException; import java.util.List; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * 返回的结果集类型 * * @author Administrator * */ public class EasyUIResult { private Integer total;// 查询返回的总行数 private List<?> rows;// 查询返回的结果集 private static final ObjectMapper MAPPER = new ObjectMapper(); public EasyUIResult() { } public EasyUIResult(Integer total, List<?> rows) { super(); this.total = total; this.rows = rows; } public EasyUIResult(Long total, List<?> rows) { super(); this.total = total.intValue(); this.rows = rows; } public Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } public List<?> getRows() { return rows; } public void setRows(List<?> rows) { this.rows = rows; } /** * 集合转换 * * @param jsonData json数据 * @param clazz 集合中的类型 * @return */ public static EasyUIResult formatToList(String jsonData, Class<?> clazz) { try { JsonNode jsonNode = MAPPER.readTree(jsonData); //获取rows的数据 JsonNode data = jsonNode.get("rows"); List<?> list = null; if (data.isArray() && data.size() > 0) { //下面代码会报错,不知道为什么,求大神指点 list = MAPPER.readValue(data.traverse(), MAPPER.getTypeFactory().constructCollectionLikeType(List.class, clazz)); } //生成对应的EasyUIResult对象 return new EasyUIResult(jsonNode.get("total").intValue(), list); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }在给出Content的javaBean
package com.taotao.manage.pojo; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Table(name = "tb_content") public class Content extends BasePojo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "category_id") private Long categoryId; private String title; @Column(name = "sub_title") private String subTitle; @Column(name = "title_desc") private String titleDesc; private String url; private String pic; private String pic2; private String content; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getCategoryId() { return categoryId; } public void setCategoryId(Long categoryId) { this.categoryId = categoryId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSubTitle() { return subTitle; } public void setSubTitle(String subTitle) { this.subTitle = subTitle; } public String getTitleDesc() { return titleDesc; } public void setTitleDesc(String titleDesc) { this.titleDesc = titleDesc; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public String getPic2() { return pic2; } public void setPic2(String pic2) { this.pic2 = pic2; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
下面是调用上面方法的测试代码
package com.taotao.taotaoweb.test; import java.io.FileReader; import java.io.IOException; import java.util.List; import java.util.Properties; import org.junit.Test; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.taotao.common.bean.EasyUIResult; import com.taotao.manage.pojo.Content; public class EasyUITest { private static final ObjectMapper MAPPER = new ObjectMapper(); @Test public void test_1() throws Exception{ Properties properties = new Properties(); //读取本地磁盘中的字符串,在下面给出文件信息 FileReader fileReader = new FileReader("D:\project\taotaoshop\test\easyUI.properties"); properties.load(fileReader); String jsonData = properties.getProperty("data"); System.out.println(jsonData); EasyUIResult.formatToList(jsonData, Content.class); } }使用的jackson的版本的坐标为
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.4.2</version> </dependency>
junit运行时,报如下的异常:
引用的本地文件说明:读取本地的文件内容如下:
data={"total":7,"rows":[{"created":1474546053000,"updated":1474546053000,"id":12,"categoryId":3,"title":"AD8","subTitle":"","titleDesc":"","url":"","pic":"http://image.taotao.com:81/images/2016/09/22/2016092208073028102522.jpg","pic2":"","content":""},{"created":1474193162000,"updated":1474193162000,"id":9,"categoryId":3,"title":"AD6","subTitle":"","titleDesc":"","url":"http://ad6","pic":"http://image.taotao.com:81/images/2016/09/18/201609180606008150399.jpg","pic2":"","content":""},{"created":1474193146000,"updated":1474193146000,"id":8,"categoryId":3,"title":"AD5","subTitle":"","titleDesc":"","url":"http://ad5","pic":"http://image.taotao.com:81/images/2016/09/18/2016091806054459107740.jpg","pic2":"","content":""},{"created":1474193127000,"updated":1474193127000,"id":7,"categoryId":3,"title":"AD4","subTitle":"","titleDesc":"","url":"http://ad4","pic":"http://image.taotao.com:81/images/2016/09/18/2016091806052540706320.jpg","pic2":"","content":""},{"created":1474193103000,"updated":1474193103000,"id":6,"categoryId":3,"title":"AD3","subTitle":"","titleDesc":"","url":"http://ad3","pic":"http://image.taotao.com:81/images/2016/09/18/2016091806050121902954.jpg","pic2":"","content":""},{"created":1474193083000,"updated":1474193083000,"id":5,"categoryId":3,"title":"AD2","subTitle":"","titleDesc":"","url":"http://ad2","pic":"http://image.taotao.com:81/images/2016/09/18/2016091806044232709620.jpg","pic2":"","content":""}]}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这样这个方法就不通用啦
应该是 Fastjson 不知道 List<?> rows 是什么类型,试试把类型明确一下