国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java 如何遍歷JsonObject對象

瀏覽:115日期:2022-08-18 13:07:02
方法:

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

補(bǔ)充:java生成json格式數(shù)據(jù) 和 java遍歷json格式數(shù)據(jù)

java 生成json 格式的數(shù)據(jù),在需要加入一個(gè)創(chuàng)建json的jar包,這個(gè)網(wǎng)上有好多,我使用的是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格式數(shù)據(jù) * @param json * @return */ public static Object traveseJson(Object json){ if(json == null){ return null; } if(json instanceof JSONObject){//json 是一個(gè)map //創(chuàng)建一個(gè)json對象 JSONObject jsonObj = new JSONObject(); //將json轉(zhuǎn)換為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 是 數(shù)組 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數(shù)據(jù)1 // {// 'QQ':['742981086@qq.com','742981086'],// 'age':22,// 'name':'aflyun',// 'hobby':['編程','看書','徒步','爬山','游泳'],// 'adderss':{'省份':'廣東','市':'惠州','國籍':'中國'}// } //創(chuàng)建 一個(gè)JsonObjec對象 JSONObject resJsonObj = new JSONObject(); //姓名 resJsonObj.put('name', 'aflyun'); //年齡 resJsonObj.put('age', 22); //聯(lián)系方式 JSONArray arryQq = new JSONArray(); arryQq.put('742981086@qq.com').put('742981086'); resJsonObj.put('QQ', arryQq); //地址 map JSONObject jsonAdress = new JSONObject(); jsonAdress.put('國籍', '中國').put('省份', '廣東').put('市', '惠州'); resJsonObj.put('adderss', jsonAdress); //生成數(shù)組array JSONArray jArray = new JSONArray(); jArray.put('編程').put('看書').put('徒步').put('爬山').put('游泳'); resJsonObj.put('hobby', jArray); System.out.println(resJsonObj); System.err.println(traveseJson(resJsonObj)); //數(shù)組類型的json格式數(shù)據(jù)生成 //[ // {'hello':'你好'}, // [ // {'在干嘛':'編程'}, // ['睡覺了嗎','沒有','不想睡','醒來了'] // ] //] JSONArray retJson = new JSONArray(); //hello JSONObject aJosn = new JSONObject(); aJosn.put('hello', '你好'); retJson.put(aJosn); //數(shù)組在干嘛和睡覺了嗎 組裝[] 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); //將組裝好的數(shù)據(jù)放入要返回的json數(shù)組中 retJson.put(jsa); System.out.println('------' + retJson); System.err.println('------' + traveseJson(retJson)); }}

通過運(yùn)行上面的代碼就能生成我們想要的json格式的數(shù)據(jù),如下所示:

{'QQ':['742981086@qq.com','742981086'],'age':22,'name':'aflyun','hobby':['編程','看書','徒步','爬山','游泳'],'adderss':{'省份':'廣東','市':'惠州','國籍':'中國'}}------[{'a':'a'},[{'b':'b'},{'c':[1,2,3]}]]{'QQ':['742981086@qq.com','742981086'],'name':'aflyun','age':22,'hobby':['編程','看書','徒步','爬山','游泳'],'adderss':{'省份':'廣東','市':'惠州','國籍':'中國'}}------[{'a':'a'},[{'b':'b'},{'c':[1,2,3]}]]

舉一反三 就可以生成我們想要的其他的json數(shù)據(jù)格式。。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 太保市| 陆川县| 清镇市| 郑州市| 仙游县| 浮山县| 临西县| 黄龙县| 花莲县| 金门县| 沾益县| 如东县| 银川市| 河北区| 仪征市| 兖州市| 长岭县| 嵩明县| 湘阴县| 嘉黎县| 仁寿县| 黄大仙区| 台江县| 四川省| 安溪县| 涟水县| 紫阳县| 临朐县| 崇文区| 枝江市| 乳源| 和林格尔县| 泰宁县| 和静县| 库尔勒市| 海门市| 明水县| 河南省| 尤溪县| 大田县| 遂昌县|