javajsonapirestgson

How to map JSON object with slashes in field name?


I am self-learned beginner in Java coding so please be understanding. I am writing REST api app that at some point expects to get such response from server:

[
    {
        "success": {
            "/config/linkbutton": true
        }
    }
]

Normally I would create specific object with field named after my expected response and I would map this response to array of this objects which I could use later in my app. Although in this case I cannot do this, because I can't of course put slashes in the name of field in class. So my question is how can I map this response to some object? Or maybe there can be another approach to use data provided in this response without creating object? I usually use gson to convert JSON to/from objects, but I am opened for all the solutions.

Will be grateful for anything that can put me in the right direction.


Solution

  • /config/linkbutton cannot, as you correctly point out, be used as a field name in Java. That is why serialization libraries typically have some way to separate the JSON name from the Java name. In GSON, this is done through the SerializedName annotation; in Jackson, the JsonProperty annotation is used, and so on.

    In your case, you could create an object with the following field:

    @SerializedName("/config/linkbutton")
    boolean configLinkbutton;