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;
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM