如何獲取json某一級節點的資料

2022-04-12 10:07:22 字數 1352 閱讀 9536

最近做專案有獲取和設定固定格式某一級節點值的需求。但是要一級一級地取對於多級的結構來說**過於冗餘且重複,於是寫了個遞迴的方法根據json路徑完成值的定點操作。廢話不多說直接貼**。

/**

* 通用的 通過json路徑找到json值

* @param jsonobject 要取值的json物件

* @param path 物件路徑

* @return 物件值列表 由於可能存在a.b.c路徑中b為列表的情況,所以結果可能有多個

*/public static listgetjsonfieldvalue(jsonobject jsonobject, string path)

return list;

}string subpath = stringutils.join(keywordlist, ".");

if (object instanceof jsonarray)

jsonarray.foreach(e -> list.addall(getjsonfieldvalue(json.parseobject(json.tojsonstring(e)), subpath)));

}else if(object instanceof jsonobject)

return list;

}/**

* 通用的 通過json路徑找到json值

* @param jsonobject 要取值的json物件

* @param path 物件路徑

* @param value 要設定的值

* @return 修改後的物件

*/public static object setjsonfieldvalue(jsonobject jsonobject, string path, object value)

object object = jsonobject.get(key);

string subpath = stringutils.join(keywordlist, ".");

if (object instanceof jsonarray)

jsonarray newarray = new jsonarray();

jsonarray.foreach(e -> newarray.add(setjsonfieldvalue(json.parseobject(json.tojsonstring(e)), subpath,value)));

jsonobject.fluentput(key,newarray);

}else if(object instanceof jsonobject)

return jsonobject;

}

zTree非同步載入展開第一級節點的實現方法

在 setting 中的 callback 中加上 onasyncsuccess onasyncsuccess 函式 然後實現 函式 var isfirst true function onasyncsuccess event,treeid 程式設計客棧 下面在給大家分享ztree獲取當前節點的下一...

Python獲取資料夾的上一級路徑

os.path.abspath os.path.join os.path.dirname settings.py os.path.pardir os.path.pardir是父目錄,os.path.abspath是絕對路徑 舉例具體看一下輸出 print os.path.dirname os.pat...

iframe中獲取上一級框架的HTML元素或JS

window.parent.document.getelementbyid得到上級元素中某個id window.parent.window.js變數名 得到上級元素js檔案 window.parent.document.getelementbyid id contentdocument.getele...