各种json报错
1. 解析错--Javabean没有实现默认的无参数构造器
Exception in thread "main" com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 22, fieldName activityDetailForm, fastjson-version 1.2.67
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:505)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:288)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:284)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:85)
2.在Javabean里写了get方法,但是没有对应成员变量和set。直接序列化时报错:logger.warn("activityWithRuleRecord = {}", JSON.toJSONString(activityWithRuleRecord));
com.alibaba.fastjson.JSONException: write javaBean error, fastjson version 1.2.67, class com.aliexpress.interactive.admin.form.ActivityDetailForm,method : getLotteryPrizePoolItem
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:539)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:149)
at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:285)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:745)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:683)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:648)
3.Javabean嵌套过深时,解析出来的数据缺失
4.指定按照javabean的成员变量序列化(默认使用set/get方法)
public static void main(String[] args) {
Sub sub = new Sub(1,"SUB");
final boolean fieldBased = true;
SerializeConfig serializeConfig = new SerializeConfig(fieldBased);
ParserConfig parserConfig = new ParserConfig(fieldBased);
Entity first = new Entity(1, "first",sub);
String str = JSON.toJSONString(first,serializeConfig);
System.out.println(str);
Entity entity = JSON.parseObject(str, Entity.class,parserConfig);
System.out.println(entity);
/*
{"code":"code","id":1,"name":"first","sub":{"code":"code","id":1,"name":"SUB"}}
Entity{id=1, name='first', sub={"code":"code","id":1,"name":"SUB"}}
{"id":1,"name":"first","sub":{"id":1,"name":"SUB"}}
null
*/
}