Java 如何遍歷JsonObject對象
Iterator iter = jsonInfo.entrySet().iterator();
代碼示例:
public class Test { public static void main(String[] args) {JSONObject jsonInfo = new JSONObject(); String key1 = 'a'; jsonInfo.put(key1, 'aa'); String key2 = 'b'; jsonInfo.put(key2, 'bb');Iterator iter = jsonInfo.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); System.out.println(entry.getKey().toString()); System.out.println(entry.getValue().toString()); } }}
補充:java生成json格式數據 和 java遍歷json格式數據
java 生成json 格式的數據,在需要加入一個創建json的jar包,這個網上有好多,我使用的是org.json的jar包。
package com.dufy.javatojson; import java.util.Iterator; import javax.sound.midi.Synthesizer; import org.json.JSONArray;import org.json.JSONObject; public class TraverseJson { /** * 遍歷json格式數據 * @param json * @return */ public static Object traveseJson(Object json){ if(json == null){ return null; } if(json instanceof JSONObject){//json 是一個map //創建一個json對象 JSONObject jsonObj = new JSONObject(); //將json轉換為JsonObject對象 JSONObject jsonStr = (JSONObject) json; //迭代器迭代 map集合所有的keys Iterator it = jsonStr.keys(); while(it.hasNext()){ //獲取map的key String key = (String) it.next(); //得到value的值 Object value = jsonStr.get(key); //System.out.println(value); //遞歸遍歷 jsonObj.put(key, traveseJson(value)); } return jsonObj; }else if(json instanceof JSONArray){// if json 是 數組 JSONArray jsonAry = new JSONArray(); JSONArray jsonStr = (JSONArray) json; //獲取Array 的長度 int length = jsonStr.length(); for (int i = 0; i <length; i++) { jsonAry.put(traveseJson(jsonStr.get(i))); } return jsonAry; }else {//其他類型 return json; } } public static void main(String[] args) { System.out.println(traveseJson('傳入要遍歷的json'));// 生成的JSON數據1 // {// 'QQ':['[email protected]','742981086'],// 'age':22,// 'name':'aflyun',// 'hobby':['編程','看書','徒步','爬山','游泳'],// 'adderss':{'省份':'廣東','市':'惠州','國籍':'中國'}// } //創建 一個JsonObjec對象 JSONObject resJsonObj = new JSONObject(); //姓名 resJsonObj.put('name', 'aflyun'); //年齡 resJsonObj.put('age', 22); //聯系方式 JSONArray arryQq = new JSONArray(); arryQq.put('[email protected]').put('742981086'); resJsonObj.put('QQ', arryQq); //地址 map JSONObject jsonAdress = new JSONObject(); jsonAdress.put('國籍', '中國').put('省份', '廣東').put('市', '惠州'); resJsonObj.put('adderss', jsonAdress); //生成數組array JSONArray jArray = new JSONArray(); jArray.put('編程').put('看書').put('徒步').put('爬山').put('游泳'); resJsonObj.put('hobby', jArray); System.out.println(resJsonObj); System.err.println(traveseJson(resJsonObj)); //數組類型的json格式數據生成 //[ // {'hello':'你好'}, // [ // {'在干嘛':'編程'}, // ['睡覺了嗎','沒有','不想睡','醒來了'] // ] //] JSONArray retJson = new JSONArray(); //hello JSONObject aJosn = new JSONObject(); aJosn.put('hello', '你好'); retJson.put(aJosn); //數組在干嘛和睡覺了嗎 組裝[] JSONArray jsa = new JSONArray(); JSONObject jOne = new JSONObject(); jOne.put('在干嘛', '編程'); JSONArray jTwo = new JSONArray(); jTwo.put('沒有').put('不想睡').put(''); JSONObject jOne1 = new JSONObject('醒來了'); jOne1.put('睡覺了嗎', jTwo); jsa.put(jOne).put(jOne1); //將組裝好的數據放入要返回的json數組中 retJson.put(jsa); System.out.println('------' + retJson); System.err.println('------' + traveseJson(retJson)); }}
通過運行上面的代碼就能生成我們想要的json格式的數據,如下所示:
{'QQ':['[email protected]','742981086'],'age':22,'name':'aflyun','hobby':['編程','看書','徒步','爬山','游泳'],'adderss':{'省份':'廣東','市':'惠州','國籍':'中國'}}------[{'a':'a'},[{'b':'b'},{'c':[1,2,3]}]]{'QQ':['[email protected]','742981086'],'name':'aflyun','age':22,'hobby':['編程','看書','徒步','爬山','游泳'],'adderss':{'省份':'廣東','市':'惠州','國籍':'中國'}}------[{'a':'a'},[{'b':'b'},{'c':[1,2,3]}]]
舉一反三 就可以生成我們想要的其他的json數據格式。。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章:
