Gson @Expose 註解可用於標記欄位是否公開(包含或不包含)以進行序列化或反序列化。 @Expose 註解 可以採用兩個參數,每個參數都是一個布林值,可以採用值 true 或 false。為了讓GSON 對@Expose 註解做出反應,我們必須使用GsonBuilder 類別建立一個Gson 實例,並且需要呼叫excludeFieldsWithoutExposeAnnotation() 方法,它將Gson 配置為排除所有沒有Expose註釋的字段進行序列化或反序列化。
語法
public GsonBuilder excludeFieldsWithoutExposeAnnotation()
範例
import com.google.gson.*; import com.google.gson.annotations.*; public class JsonExcludeAnnotationTest { public static void main(String args[]) { Employee emp = new Employee("Raja", 28, 40000.00); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String jsonStr = gson.toJson(emp); System.out.println(jsonStr); gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); jsonStr = gson.toJson(emp); System.out.println(jsonStr); } } // Employee class class Employee { @Expose(serialize = true, deserialize = true) public String name; @Expose(serialize = true, deserialize = true) public int age; @Expose(serialize = false, deserialize = false)<strong> </strong> public double salary; public Employee(String name, int age, double salary) { this.name = name; this.age = age; this.salary = salary; } }
輸出
{ "name": "Raja", "age": 28, "salary": 40000.0 } { "name": "Raja", "age": 28 }
以上是如何在Java中使用@Expose註解從JSON中排除一個欄位?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能