简体   繁体   中英

De-serializing JSON object with property named 'return'

Working Java, using Gson. The input starts:

{"success":1,"return":{   

Unfortunately, as you know, you can not create a field/variable named 'return' in Java. So, how does one get around this, as the bulk of the data is behind the 'return' field?

This question Serialize JSON object named "return" is related, but the language is C#.

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

public class Whatever {
    private int success;        

    @SerializedName("return")
    private OtherType returnValue;
    ...
}

Annotate your field with

@SerializedName("return")
private SomeType doesntMatter;

Gson will use the value given to the @SerializedName annotation to map your field by name.

You can use Java annotations of Gson @SerializedName("return") .

@SerializedName("return")
private String returnField;

About Gson annotations you can read here .

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