1. 程式人生 > 程式設計 >Java 如何遍歷JsonObject物件

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]","age":22,"name":"aflyun","hobby":["程式設計","adderss":{"省份":"廣東","國籍":"中國"}}
------[{"a":"a"},[{"b":"b"},{"c":[1,2,3]}]]
{"QQ":["[email protected]",3]}]]

舉一反三 就可以生成我們想要的其他的json資料格式。。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。