簡體   English   中英

Gson Java 保留關鍵字

[英]Gson Java reserved keyword

我有一些 JSON 正在使用 Gson 反序列化。

{
"resp": {
"posts": [
  {
    ...
    "public": true,
    ...
  }] 
}

我的問題是public是一個 Java 關鍵字,那么如何在我的 class 中創建一個與 JSON 中的public字段相關的字段?

您可以使用 gson 的Field Naming Support為您的字段使用不同的名稱。

public class Post {
    @SerializedName("public")
    private boolean isPublic;
    ...
}

Worth a quick note that you need to include gson.annotations or SerializedName for this to compile as not part of the base gson.Gson package:

import com.google.gson.annotations.SerializedName;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM