json 字符串转 map集合

一、json 字符串转 map集合,主要是通过迭代器遍历json,然后再把 键值对逐个put() 进map 集合

1. 先导入maven依赖

<dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>fastjson</artifactId>
       <version>1.2.32</version>
</dependency>   

2. json 字符串转 map集合

// json转map集合
    public static Map<String, String> jsonToMap(String json){
        // json字符串转JSONObject对象
        JSONObject jsonObject = JSONObject.parseObject(json);
        Iterator<Map.Entry<String, Object>> iterator = jsonObject.entrySet().iterator();
        HashMap<String, String> map = new HashMap<>();
        while (iterator.hasNext()){
            Map.Entry<String, Object> next = iterator.next();
            map.put(next.getKey(), next.getValue().toString());
        }
        return map;
    }

猜你喜欢

转载自www.cnblogs.com/unknows/p/12765593.html