Jackson - Recursive parsing into Map

后端 未结 1 1827
旧时难觅i
旧时难觅i 2020-12-05 08:31

I\'m trying to simplify my code: I want to store key and values (all strings).

I\'m actually using a Map to store it. hat way

相关标签:
1条回答
  • 2020-12-05 08:59

    Assuming that your end goal is just to deserialize JSON into a Map<String, Object>, there is a far simpler way to do this with Jackson. Using ObjectMapper:

    final String json = "{}";
    final ObjectMapper mapper = new ObjectMapper();
    final MapType type = mapper.getTypeFactory().constructMapType(
        Map.class, String.class, Object.class);
    final Map<String, Object> data = mapper.readValue(json, type);
    

    You will need error handling etc, but this is a good starting point.

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