user15296624
user15296624

Reputation:

@JsonPoperty can not get nested value

I have:

    {
        "id": "2021-04-03T15-SV_Waldhof_Mannheim--Zwickau",
        "something": {
            "id": "12",
            "value": 1.5
        }
    }

I want get value: 1.15, and store it in my variable. How can i do it with @JsonPropety?

    @JsonProperty("something[value}") //how to do it correctly?
    private float value;

How i parse JSON:

restTemplate.exchange(MY_GET_REQUEST, HttpMethod.GET, entity, new ParameterizedTypeReference<List<MyEntity>>(){})

I will be grateful for any help, if you know identical topics - just send link

UPDATED something.value does not work

The same problem with unpacking, such as:


 @JsonProperty("something")
    public void setLng(Map<String, Float> coordinates) {
        this.value= (Float.parseFloat(coordinates.get("value")));
 }

Also does not work

Upvotes: 0

Views: 106

Answers (1)

ZhenyaM
ZhenyaM

Reputation: 686

You have 2 options:

  1. Use custom deserializer for your response. In this case you able to populate any target DTO in any way. Here you could find example of custom deserializer
  2. Use the same structure for your DTO as in response (with sub object) and add additional method in root DTO to access this value. But in this case it could produce side effects on serialization (for example, additional field in root DTO)

UPDATE Such configuration is working for me

    public static class Obj {
        @JsonProperty("id")
        String id;
        Float value;

        @JsonProperty("something")
        public void value(Map<String, Object> obj) {
            this.value = Float.parseFloat(obj.get("value").toString());
        }

    }

Upvotes: 1

Related Questions