Java – Jackson mapping: deserializing JSON with different attribute names

I have a server that returns JSON strings:

{"pId": "ChIJ2Vn0h5wOlR4RsOSteUYYM6g"}

Now, I can use Jackson to deserialize it as an object of a variable named PID, but I don't want the variable to be called PID. I'd rather deserialize it as placeid

Current object in Android Java:

public class Place {

    private String pId;

}

What do I want the object to look like:

public class Place {

    private String placeId;

}

If I change the variable of the object to placeid, Jackson will not be able to deserialize JSON because the property names no longer match

Is there a Jackson annotation that I can use to map the "placeid" variable in the Java object to the JSON string variable "PID" returned from the server?

Solution

Use @ jsonproperty annotation:

public class Place {

    @JsonProperty("pId")
    private String placeId;

}

For more information, you can see the relevant Javadoc

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>