pojo parse gson with invalid java names

后端 未结 3 952
滥情空心
滥情空心 2021-01-04 03:51

i\'m working with the youtube json from google-api-client :

{
    \"apiVersion\": \"2.0\",
    \"data\": {
        \"updated\": \"2011-01-05T13:48:33.146Z\",         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 04:47

    I believe your answer lies in the JSON Field Naming Support:

    Gson supports some pre-defined field naming policies to convert the standard Java field names (i.e. camel cased names starting with lower case --- "sampleFieldNameInJava") to a Json field name (i.e. sample_field_name_in_java or SampleFieldNameInJava).

    See for instance the following example:

    private class SomeObject {
      @SerializedName("custom_naming") private final String someField;
      private final String someOtherField;
    
      public SomeObject(String a, String b) {
        this.someField = a;
        this.someOtherField = b;
      }
    }
    

    So you should be able to define the field mapping to the default value like this:

    @SerializedName("default")
    private final String someOtherNameThanDefault;
    

提交回复
热议问题