简体   繁体   English

Gson Java 保留关键字

[英]Gson Java reserved keyword

I have some JSON that I am deserializing using Gson.我有一些 JSON 正在使用 Gson 反序列化。

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

My problem is that public is a Java keyword, so how would I make a field in my class that correlates with the public field in the JSON?我的问题是public是一个 Java 关键字,那么如何在我的 class 中创建一个与 JSON 中的public字段相关的字段?

You could use a different name for your field, using gson's Field Naming Support .您可以使用 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: 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.

相关问题 如何在Java中使用保留关键字声明参数名称 - How to declare a parameter name with a reserved keyword in Java Thrift Java:不能使用保留语言关键字:“别名” - Thrift Java: Cannot use reserved language keyword: “alias” _(下划线)是保留关键字 - _ (underscore) is a reserved keyword 在ANTLR中重用保留关键字规则 - Reuse a reserved keyword rule in ANTLR 错误属性名称是保留关键字 - error Attribute name is a reserved keyword 休眠保留关键字转义不起作用 - Hibernate Reserved keyword escaping not working 如何更新 Dynamo DB 中保留关键字的值。 错误:属性名称是保留关键字; 保留关键字:数据 - How to update a value for reserved keyword in Dynamo DB. Error: Attribute name is a reserved keyword; reserved keyword: data Java 10 中“受限关键字”和“保留类型名称”之间的概念区别是什么? - What is the conceptual difference between a “restricted keyword” and “reserved type name” in Java 10? 在Java编译器中,可以将哪种类型定义为标识符(ID)或关键字(保留字)? - In Java compiler,which type can be defined as identifier ( ID ) or Keyword (reserved word)? 使用Gson的包装关键字反序列化json - Deserialze json with a wrapper keyword with Gson
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM