环境
测试类中代码
@Test
public void jsonTest(){
Brand brand=new Brand(1,"三只松鼠","三只松鼠","good");
String jsonString = JSON.toJSONString(brand);
System.out.println(jsonString);
Brand b = JSON.parseObject(jsonString,Brand.class);
System.out.println(b);
}
pom.xml中依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.6</version>
</dependency>
当json字符串转换为Java对象时报错
解决方法
检查实体类中是否有无参构造方法,单只有有参构造方法是不行的,添加即可
public Brand() {
}