测试代码
@Test
void contextLoads() {
//模拟数据
List<BrandEntity> l=new ArrayList();
BrandEntity b1=new BrandEntity();
BrandEntity b2=new BrandEntity();
b1.setBrandId(1l);
b2.setBrandId(2l);
l.add(b1);
l.add(b2);
//处理简单类型
String s1 = JSON.toJSONString(b1);
BrandEntity brandEntity = JSON.parseObject(s1, BrandEntity.class);
System.out.println(JSON.toJSONString(brandEntity));
//fastjson对复杂类型的处理 TypeReference<T>
TypeReference<List<BrandEntity>> brandEntityTypeReference = new TypeReference<List<BrandEntity>>(){};
String s = JSON.toJSONString(l);
List<BrandEntity> brandEntities = JSON.parseObject(s, brandEntityTypeReference);
System.out.println(JSON.toJSONString(brandEntities));
}
运行结果:
{"brandId":1}
[{"brandId":1},{"brandId":2}]