Collecting unknown properties with Jackson

前端 未结 1 460
挽巷
挽巷 2020-12-10 04:08

I\'m using Jackson for creating Java objects from JSON. Let\'s suppose I have a JSON string like this:

{\"a\":\"a\", \"b\":\"b\", \"c\":\"c\"}
相关标签:
1条回答
  • 2020-12-10 04:25

    I don't know of any built-in tool that does this. You can write your own with @JsonAnySetter

    Marker annotation that can be used to define a non-static, two-argument method (first argument name of property, second value to set), to be used as a "fallback" handler for all otherwise unrecognized properties found from JSON content.

    Use it like

    @JsonAnySetter
    public void ignored(String name, Object value) {
        // can ignore the 'value' if you only care for the name (though you still need the second parameter)
        System.out.println(name + " : " + value);
    }
    

    within the class you're deserializing to, eg. your A class.

    0 讨论(0)
提交回复
热议问题