How to get a value from a JSON string using jackson library?

后端 未结 3 906
余生分开走
余生分开走 2021-01-02 00:59

I am trying to get a value from a JSON string but I am getting a null value instead.

App2.java :

package JsonExample1;

import org.codehaus.jackson.J         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 01:03

    Convert to the Map

    Map map = objectMapper.readValue(jsonString, Map.class);
    

    Method for navigation in the map

    private static  T get(Map map, Class clazz, String... path) {
        Map node = map;
        for (int i = 0; i < path.length - 1; i++) {
          node = (Map) node.get(path[i]);
        }
        return (T) node.get(path[path.length - 1]);
      }
    

    Usage:

    String value = get(map, String.class, "path", "to", "the", "node")
    

提交回复
热议问题