GSON将Java对象转成JSON时,如何排除某些字段

    1. GSON 是Google发布的 JSON 序列化/反序列化工具,非常容易使用。本文简要讨论在使用GSON将Java对象转成JSON时,如何排除某些字段。

      最简单的用法

      假设有下面这个类:

       1 class MyObj {
       2   
       3   public int x;
       4   public int y;
       5   
       6   public MyObj(int x, int y) {
       7       this.x = x;
       8       this.y = y;
       9   }
      10 }

       

      最简单的GSON用法如下所示:
      @Test
          public void gson() {
              MyObj obj = new MyObj(1, 2);
              String json = new Gson().toJson(obj);
              Assert.assertEquals("{\"x\":1,\"y\":2}", json);
          }

       

       
      方法1:排除transient字段

      这个方法最简单,给字段加上 transient 修饰符就可以了,如下所示: 

      class MyObj {
        
        public transient int x; // <---
        public int y;
        
        public MyObj(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
          }
      @Test
          public void gson() {
              MyObj obj = new MyObj(1, 2);
              String json = new Gson().toJson(obj);
              Assert.assertEquals("{\"y\":2}", json); // <---
          }

       

      方法2:排除Modifier为指定类型的字段

      这个方法需要用GsonBuilder定制一个GSON实例,如下所示:

      class MyObj {
        
        protected int x; // <---
        public int y;
        
        public MyObj(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
      }
      @Test
          public void gson() {
          Gson gson = new GsonBuilder()
              .excludeFieldsWithModifiers(Modifier.PROTECTED) // <---
              .create();
        
            MyObj obj = new MyObj(1, 2);
            String json = gson.toJson(obj); // <---
            Assert.assertEquals("{\"y\":2}", json);
      }
          

       

      方法3:使用@Expose注解

      注意,没有被 @Expose 标注的字段会被排除,如下所示: 

      class MyObj {
        
        public int x;
        @Expose public int y; // <---
        
        public MyObj(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
          }
      @Test
          public void gson() {
        Gson gson = new GsonBuilder()
          .excludeFieldsWithoutExposeAnnotation() // <---
          .create();
        
        MyObj obj = new MyObj(1, 2);
        String json = gson.toJson(obj);
        Assert.assertEquals("{\"y\":2}", json);
          }

       

      方法4:使用ExclusionStrategy定制字段排除策略

      这种方式最灵活,下面的例子把所有以下划线开头的字段全部都排除掉:

      class MyObj {
        
        public int _x; // <---
        public int y;
        
        public MyObj(int x, int y) {
            this._x = x;
            this.y = y;
        }
        
          }
      @Test
          public void gson() {
        ExclusionStrategy myExclusionStrategy = new ExclusionStrategy() {
      
            @Override
            public boolean shouldSkipField(FieldAttributes fa) {
          return fa.getName().startsWith("_");
            }
      
            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
          return false;
            }
            
        };
        
        Gson gson = new GsonBuilder()
          .setExclusionStrategies(myExclusionStrategy) // <---
          .create();
        
        MyObj obj = new MyObj(1, 2);
        String json = gson.toJson(obj);
        Assert.assertEquals("{\"y\":2}", json);
          }

       

      来自http://blog.csdn.net/zxhoo/article/details/21471005

       

       

       

转载于:https://www.cnblogs.com/xhyper/p/3949546.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值