java中JSONObject轉(zhuǎn)換為HashMap(方法+main方法調(diào)用實(shí)例)
1.首先要導(dǎo)入json相關(guān)的jar包
引入的jar包:
(版本自行定義,可以選用使用人數(shù)偏多的版本,這樣比較穩(wěn)定)
commons-beanutils-1.9.2.jar
commons-collections-3.2.1.jar
commons-lang-2.6.jar
commons-logging-1.2.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar
jar包的下載可以去下面這個網(wǎng)址搜索:
https://mvnrepository.com/
2.在Eclipse下(也可以是IntelliJ IDEA或者M(jìn)yEclipse)
新建package和Class(步驟略過,可自行選擇名字),這里就使用jsonTest。
以下代碼塊方法見注釋,是將JSONObject轉(zhuǎn)換為HashMap的主要方法,傳入?yún)?shù)為一個JSONObject對象,返還值為一個HashMap。
//1.將JSONObject對象轉(zhuǎn)換為HashMap<String,String> public static HashMap<String, String> JsonObjectToHashMap(JSONObject jsonObj){ HashMap<String, String> data = new HashMap<String, String>(); Iterator it = jsonObj.keys(); while(it.hasNext()){ String key = String.valueOf(it.next().toString()); String value = (String)jsonObj.get(key).toString(); data.put(key, value); } System.out.println(data); return data; }
這個方法是將JSON字符串轉(zhuǎn)換為HashMap,傳入?yún)?shù)為一段json格式的字符串,返還一個HashMap。
//2.將json字符串轉(zhuǎn)換成HashMap<String,String> public static HashMap<String, String> JsonToHashMap(String JsonStrin){ HashMap<String, String> data = new HashMap<String, String>(); try{ // 將json字符串轉(zhuǎn)換成jsonObject JSONObject jsonObject = JSONObject.fromObject(JsonStrin); @SuppressWarnings("rawtypes") Iterator it = jsonObject.keys(); // 遍歷jsonObject數(shù)據(jù),添加到Map對象 while (it.hasNext()) { String key = String.valueOf(it.next()).toString(); String value = (String) jsonObject.get(key).toString(); data.put(key, value); } }catch (Exception e) { e.printStackTrace(); //JOptionPane.showMessageDialog(null,"ERROR:["+e+"]"); } System.out.println(data); return data; }
在這里順便介紹一下Iterator類(迭代器)
迭代器是一種設(shè)計(jì)模式,它是一個對象,它可以遍歷并選擇序列中的對象,而開發(fā)人員不需要了解該序列的底層結(jié)構(gòu)。迭代器通常被稱為“輕量級”對象,因?yàn)閯?chuàng)建它的代價小。
Java中的Iterator功能比較簡單,并且只能單向移動:
(1) 使用方法iterator()要求容器返回一個Iterator。第一次調(diào)用Iterator的next()方法時,它返回序列的第一個元素。注意:iterator()方法是java.lang.Iterable接口,被Collection繼承。
(2) 使用next()獲得序列中的下一個元素。
(3) 使用hasNext()檢查序列中是否還有元素。
(4) 使用remove()將迭代器新返回的元素刪除。
Iterator是Java迭代器最簡單的實(shí)現(xiàn),為List設(shè)計(jì)的ListIterator具有更多的功能,它可以從兩個方向遍歷List,也可以從List中插入和刪除元素。
3.直接上代碼
package JSON; import java.util.HashMap; import java.util.Iterator; import net.sf.json.JSONObject; public class JsonTest { public static void main(String[] args) { JSONObject jsonObj = new JSONObject(true); String content1 = "aaaaa"; String content2 = "bbbbb"; String content3 = "ccccc"; jsonObj.put("a", content1); jsonObj.put("b", content2); jsonObj.put("c", content3); System.out.println(jsonObj.toString()); JsonObjectToHashMap(jsonObj); String jsonstr = "{name:'王楊',sex:'男',school:'鄭州航空工業(yè)管理學(xué)院'}"; JsonToHashMap(jsonstr); } //1.將JSONObject對象轉(zhuǎn)換為HashMap<String,String> public static HashMap<String, String> JsonObjectToHashMap(JSONObject jsonObj){ HashMap<String, String> data = new HashMap<String, String>(); Iterator it = jsonObj.keys(); while(it.hasNext()){ String key = String.valueOf(it.next().toString()); String value = (String)jsonObj.get(key).toString(); data.put(key, value); } System.out.println(data); return data; } //2.將json字符串轉(zhuǎn)換成HashMap<String,String> public static HashMap<String, String> JsonToHashMap(String JsonStrin){ HashMap<String, String> data = new HashMap<String, String>(); try{ // 將json字符串轉(zhuǎn)換成jsonObject JSONObject jsonObject = JSONObject.fromObject(JsonStrin); @SuppressWarnings("rawtypes") Iterator it = jsonObject.keys(); // 遍歷jsonObject數(shù)據(jù),添加到Map對象 while (it.hasNext()) { String key = String.valueOf(it.next()).toString(); String value = (String) jsonObject.get(key).toString(); data.put(key, value); } }catch (Exception e) { e.printStackTrace(); //JOptionPane.showMessageDialog(null,"ERROR:["+e+"]"); } System.out.println(data); return data; } }
記得修改自己的package名稱和 class名稱。
4.調(diào)用main方法測試
(1)傳入?yún)?shù)為JSONObject:
輸出結(jié)果為:
(2)傳入?yún)?shù)為JSON字符串:
輸出結(jié)果為:
這里可以看到,輸出的參數(shù)順序和傳入時正好相反。但是輸出類型為HashMap,數(shù)據(jù)存儲的格式是以key-value鍵值對的形式存數(shù)于HashMap中的。我們可以通過獲取key值來獲取到其對應(yīng)的value。
增加如下代碼在main方法最后面:
System.out.println("");//空格換行 //通過對應(yīng)的key鍵值,獲取value HashMap<String,String> hashmap = JsonToHashMap(jsonstr); System.out.println("--------通過遍歷HashMap輸出值:-------"); System.out.println("name:"+hashmap.get("name")+",sex:"+ hashmap.get("sex")+",school:"+hashmap.get("school"));
得到如下結(jié)果:
結(jié)語:
到此基本的方法介紹完畢,其實(shí)是依靠了JSONObject這個對象的fromObject()方法。fromObject()方法可以轉(zhuǎn)換的類型很多,可以是map、list、數(shù)組等等。運(yùn)用在自己的項(xiàng)目中時,可以是bean或者model等自定義的類。
1. List集合轉(zhuǎn)換成json代碼 List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray2 = JSONArray.fromObject( list ); 2. Map集合轉(zhuǎn)換成json代碼 Map map = new HashMap(); map.put("name", "json"); map.put("bool", Boolean.TRUE); map.put("int", new Integer(1)); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr[i]; }"); JSONObject json = JSONObject.fromObject(map); 3. Bean轉(zhuǎn)換成json代碼 JSONObject jsonObject = JSONObject.fromObject(new JsonBean()); 4. 數(shù)組轉(zhuǎn)換成json代碼 boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
以上類型均可以借用fromObject()方法轉(zhuǎn)換為一個JSONObject類型實(shí)例。
json作為輕量級的數(shù)據(jù)格式,在前后端數(shù)據(jù)交互時很常見,每個公司應(yīng)該都有自己的JSON轉(zhuǎn)換方法,是公司常見的工具類。
方便了隨后的開發(fā)使用。
到此這篇關(guān)于java中JSONObject轉(zhuǎn)換為HashMap(方法+main方法調(diào)用實(shí)例)的文章就介紹到這了,更多相關(guān)JSONObject轉(zhuǎn)換為HashMap內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java中JSONObject和Map<String,?Object>的轉(zhuǎn)換方法
- Java如何獲取JSONObject內(nèi)指定字段key的value值
- Java中如何將String轉(zhuǎn)JSONObject
- java中如何判斷JSONObject是否存在某個Key
- Java使用fastjson對String、JSONObject、JSONArray相互轉(zhuǎn)換
- Java 如何遍歷JsonObject對象
- JAVA中JSONObject對象和Map對象之間的相互轉(zhuǎn)換
- Java使用JSONObject需要的6個jar包下載地址
- 詳解Java中String JSONObject JSONArray List<實(shí)體類>轉(zhuǎn)換
- Java使用JSONObject操作json實(shí)例解析
- Java中的JSONObject使用方法和常用操作
相關(guān)文章
Java基于Dijkstra算法實(shí)現(xiàn)校園導(dǎo)游程序
這篇文章主要為大家詳細(xì)介紹了Java基于Dijkstra算法實(shí)現(xiàn)校園導(dǎo)游程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03取消idea雙擊shift鍵時出現(xiàn)的全局搜索的問題分析
這篇文章主要介紹了取消idea雙擊shift鍵時出現(xiàn)的全局搜索的問題分析,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10Java中controller層如何接收帶參數(shù)的查詢
本文主要介紹了Java中controller層如何接收帶參數(shù)的查詢,在控制器層接收帶參數(shù)的查詢可以通過多種方式實(shí)現(xiàn),下面就詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08spring?eurake中使用IP注冊及問題小結(jié)
在開發(fā)spring?cloud的時候遇到一個很奇葩的問題,就是服務(wù)向spring?eureka中注冊實(shí)例的時候使用的是機(jī)器名,然后出現(xiàn)localhost、xxx.xx等這樣的內(nèi)容,這篇文章主要介紹了spring?eurake中使用IP注冊,需要的朋友可以參考下2023-07-07SpringMVC Mock測試實(shí)現(xiàn)原理及實(shí)現(xiàn)過程詳解
這篇文章主要介紹了SpringMVC Mock測試實(shí)現(xiàn)原理及實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10