class A declares multiple JSON fields

前端 未结 8 746
温柔的废话
温柔的废话 2020-12-01 02:21

i have a class A which has some private fields and the same class extends another class B which also has some private fields which are in class A.

public cla         


        
8条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 03:21

    I used GsonBuilder and ExclusionStrategy to avoid the redundant fields as below, it is simple and straight forward.

    Gson json = new GsonBuilder()
              .setExclusionStrategies(new ExclusionStrategy() {
                 @Override
                 public boolean shouldSkipField(FieldAttributes f) {
                    if(f.getName().equals("netAmountPcy")){
                       return true;
                    }
                    return false;
                 }
    
                 @Override
                 public boolean shouldSkipClass(Class clazz) {
                    return false;
                 }
              }).create();
    

提交回复
热议问题