简体   繁体   中英

Gson Java reserved keyword

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?

You could use a different name for your field, using gson's 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM