各种json报错
Posted 为爱奋斗不息
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了各种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
*/
}
以上是关于各种json报错的主要内容,如果未能解决你的问题,请参考以下文章
错误记录Flutter 混合开发获取 BinaryMessenger 报错 ( FlutterActivityAndFragmentDelegate.getFlutterEngine() )(代码片段
vbscript 各种自定义代码片段 - 有关详细信息,请参阅注释