Flatten a JSON string to make the key including each level key value to Map using Gson or Jackson

前端 未结 2 776
醉梦人生
醉梦人生 2021-01-19 05:42

I have an enhanced question regarding Flatten a JSON string to Map using Gson or Jackson.

My scenario included duplicated keys, so the solution in the above questio

2条回答
  •  Happy的楠姐
    2021-01-19 06:37

    I resolved this using below simple code, Only think is need to download jettison and flattener.JsonFlattener library

    import java.util.Map;
    import org.codehaus.jettison.json.JSONObject;
    import com.github.wnameless.json.flattener.JsonFlattener;
    
    public class test {
    
        public static void main(String[] args) {
            String jsonString = "{\"id\" : \"123\",\"name\" : \"Tom\",\"class\" : {\"subject\" : \"Math\",\"teacher\" : \"Jack\"}}";
            JSONObject jsonObject = new JSONObject();
            String flattenedJson = JsonFlattener.flatten(jsonString);
            Map flattenedJsonMap = JsonFlattener.flattenAsMap(jsonString);
            System.out.println(flattenedJsonMap);
        }
    }
    

    Reference link : https://github.com/wnameless/json-flattener

提交回复
热议问题