Java實現(xiàn)map轉(zhuǎn)換成json的方法詳解
更新時間:2022年05月27日 10:26:36 作者:web18296061989
這篇文章主要為大家詳細(xì)介紹了Java語言實現(xiàn)map轉(zhuǎn)換成json的幾種方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
1.alibaba falstjson
1.Map轉(zhuǎn)JSON
Map<String, Object> map = new HashMap<String, Object>(); map.put("a", "a"); map.put("b", "123"); JSONObject json = new JSONObject(map);
2.map轉(zhuǎn)string
Map<String, Object> map = new HashMap<>(); map.put("a", "b"); String s = JSONObject.toJSONString(map);
3.JSON轉(zhuǎn)String
JSONObject json = new JSONObject(); json.put("c", "v"); json.put("z", "123n); json.toJSONString();
4.JSON轉(zhuǎn)Map
JSONObject json = new JSONObject(); json.put("ccc", "321"); json.put("bbb", "123"); Map<String, Object> map = (Map<String, Object>)json;
5.String轉(zhuǎn)JSON
String str = "{"username":"dsad","qwewqe":"123"}"; JSONObject json = JSONObject.parseObject(str);
2.google
maven坐標(biāo)
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency>
Map轉(zhuǎn)換成JSON
Map<String,String> map = new HashMap<String,String>(); map.put("a","aaa"); map.put("b","bbb"); map.put("c","ccc"); String json=JSON.toJSONString(map); System.out.println(json);//輸出{"a":"aaa","b":"bbb","c":"ccc"}
JSON轉(zhuǎn)換成Map
Map map1 = JSON.parseObject(json); System.out.println(map1.get("a")); for (Object mapData : map.entrySet()) { Map.Entry<String,String> entry = (Map.Entry<String,String>)mapData; System.out.println(entry.getKey()+"--->"+entry.getValue()); } /*輸出 b--->bbb c--->ccc a--->aaa */
map中含有對象Map -> JSON
//Map -> JSON Map<String,Bar> map = new HashMap<String, Bar>(); map.put("a",new Bar()); map.put("b",new Bar()); map.put("c",new Bar()); String json = JSON.toJSONString(map,true); System.out.println(json); /* 輸出{ "a":{ "barAge":383687382, "barDate":1494945882018, "barName":"name_1689176802" }, "b":{ "barAge":-100528778, "barDate":1494945882018, "barName":"name_-878176366" }, "c":{ "barAge":-344075192, "barDate":1494945882018, "barName":"name_-1710740534" } } */
JSON -> Map
Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json); for (String key : map1.keySet()) { System.out.println(key+":"+map1.get(key)); } /*輸出 b:{"barAge":-100528778,"barDate":1494945882018,"barName":"name_-878176366"} c:{"barAge":-344075192,"barDate":1494945882018,"barName":"name_-1710740534"} a:{"barAge":383687382,"barDate":1494945882018,"barName":"name_1689176802"} */
附–MAP的ASCII排序
StringBuilder sb = new StringBuilder(); List<String> keys = new ArrayList<String>(map.keySet()); Collections.sort(keys);//排序。 for(String k : keys){ String v = params.get(k); if(StringUtils.isNotEmpty(v)){ sb.append(v); } } //return MD5.toMD5(sb+key, "UTF-8");這個就不用看了~~~
到此這篇關(guān)于Java實現(xiàn)map轉(zhuǎn)換成json的方法詳解的文章就介紹到這了,更多相關(guān)Java map轉(zhuǎn)json內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA2020如何打開Run Dashboard的方法步驟
這篇文章主要介紹了IDEA2020如何打開Run Dashboard的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07tio-boot框架整合ehcache實現(xiàn)過程示例
這篇文章主要為大家介紹了tio-boot框架整合ehcache實現(xiàn)過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12mybatis插件優(yōu)雅實現(xiàn)字段加密的示例代碼
在很多時候,我們都需要字段加密,比如郵箱,密碼,電話號碼等,本文主要介紹了mybatis插件優(yōu)雅實現(xiàn)字段加密的示例代碼,感興趣的可以了解一下2023-11-11SpringBoot整合screw實現(xiàn)數(shù)據(jù)庫文檔自動生成的示例代碼
這篇文章主要介紹了SpringBoot整合screw實現(xiàn)數(shù)據(jù)庫文檔自動生成的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09