[英]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.