Gson Java reserved keyword

2019-01-06 19:04发布

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?

2条回答
劳资没心,怎么记你
2楼-- · 2019-01-06 19:16

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;
查看更多
神经病院院长
3楼-- · 2019-01-06 19:34

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

public class Post {
    @SerializedName("public")
    private boolean isPublic;
    ...
}
查看更多
登录 后发表回答