Gson Java reserved keyword

杀马特。学长 韩版系。学妹 提交于 2019-12-17 06:46:08

问题


I have some JSON that I am deserializing using 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?


回答1:


You could use a different name for your field, using gson's Field Naming Support.

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



回答2:


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;


来源:https://stackoverflow.com/questions/6258796/gson-java-reserved-keyword

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!