GSON將Java對象轉成JSON時,如何排除某些字段


  • 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

     

     

     

  • 免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM