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