zoukankan      html  css  js  c++  java
  • 后台对象,使用google json组件用json格式返回给前端的问题

    public class A{
        private int id;
        pirvate Date createTime
        private B b;   
    }
    public class B{
        private int id;
        private String name;
        private Date createTime;
    }

    此时,如果用google json组件返回A对象时,会报declares multiple JSON fields named

    异常,出现这个异常是因为,两个类都有createTime同名属性。

    解决方案:

    public class SomeClassWithFields {
       @SerializedName("name") private final String someField;
       private final String someOtherField;
    
       public SomeClassWithFields(String a, String b) {
         this.someField = a;
         this.someOtherField = b;
       }
     }
    ===== OUTPUT =====
     {"name":"a","someOtherField":"b"}

    http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/annotations/SerializedName.html
  • 相关阅读:
    Flex布局-语法篇
    css grid布局
    js 方法的一些简写和技巧
    css瀑布流
    js防抖和节流
    js循环
    两行css代码实现居中元素
    手写代码部分
    BigDecimal类的概念和使用
    Math类的概念和使用
  • 原文地址:https://www.cnblogs.com/dingchenghong/p/2840076.html
Copyright © 2011-2022 走看看