• 各种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

    */

    }
  • 相关阅读:
    golang并发
    golang接口
    golang方法
    golang函数
    微信小程序请求封装
    使用vue实现打印功能时出现多余空白页的问题
    mybatis 基本配置
    sql调优
    触发器 索引
    收藏 故事形式讲解javaScript中创建对象和Java创建对象的区别
  • 原文地址:https://www.cnblogs.com/ctaixw/p/13300424.html
Copyright © 2020-2023  润新知