javascript
JSON树节点的增删查改
最近了解到使用json字符串存到數據庫的一種存儲方式,取出來的json字符串可以進行相應的節點操作
故借此機會練習下遞歸,完成對json節點操作對應的工具類。
介紹一下我使用的依賴
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.49</version> </dependency>主要使用JSONObject和JSONArray的API進行相關操作,這里附上這兩個類的代碼
JSONObject
JSONArray
一些數據轉換的API
JSONArray.parseArray(“json樹字符串”)-----------可以返回JSONArray對象
jsonArray對象.toJSONString()------------------------可以轉換為字符串便于存入數據庫
首先我們需要有一個json樹,這里可以自己編寫,跟數據庫操作的相關方法暫不涉及,這里直接使用相關API搭建,在main方法中
public static void main(String[] args) {JSONArray details=new JSONArray();JSONObject tree1=new JSONObject();tree1.put("id",1);tree1.put("code", "taosir");tree1.put("name", "taosir");JSONObject tree2=new JSONObject();tree2.put("id",2);tree2.put("code", "moer");tree2.put("name", "moer");JSONArray array1=new JSONArray();array1.add(tree1);array1.add(tree2);JSONObject tree3=new JSONObject();tree3.put("id",3);tree3.put("code", "xixi");tree3.put("name", "xixi");tree3.put("children", array1);JSONObject tree4=new JSONObject();tree4.put("id",4);tree4.put("code", "jack");tree4.put("name", "jack");JSONArray array2=new JSONArray();array2.add(tree3);array2.add(tree4);JSONObject tree5=new JSONObject();tree5.put("id",5);tree5.put("code", "lay");tree5.put("name", "lay");tree5.put("children", array2);JSONObject tree6=new JSONObject();tree6.put("id",6);tree6.put("code", "haer");tree6.put("name", "haer");details.add(tree5);details.add(tree6);System.out.println(details); }生成的json樹
點擊上面可以查看生成的json樹
OK,準備工作完畢,下面進行功能演示。
(注意,每演示一個功能點,請注釋掉其他的打印語句)
首先是查詢:
/*** 根據單一條件獲取節點位置* @param body 查詢目標的主體內容* @param key 篩選匹配條件條件對應--key* @param value 篩選匹配條件對應--value* @param result 緩存查詢結果* @return*/public static JSONObject getNode(JSONArray body,String key,Object value,JSONObject result) {for (int i = 0; i < body.size(www.dfgjpt.com); i++) {JSONObject jsonObject =body.getJSONObject(i);if (jsonObject.get(key).toString(www.thd540.com).equals(value.toString())) {for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {result.put(entry.getKey(), entry.getValue());}}else if(jsonObject.getJSONArray("children")!=null) {getNode(jsonObject.getJSONArray("children"), key, value,result);}}return result;}在main方法調用演示,將前面的打印注釋掉
//System.out.println(details); System.out.println(getNode(details, "id", 4,new JSONObject()));查詢寫出來,基本思路對了,其他的操作都類似,簡單得多
下面是添加
/*** * @param body 需要添加的目標樹主體* @param key 篩選匹配條件對應的key* @param value 篩選匹配條件的值* @param index 需要插入的下標位置索引* @param node 插入的整體節點*/public static void addNode(JSONArray body,String key,Object value,int index,JSONObject node) {for (int i = 0; i < body.size(); i++) {if("id".equals(key)&&"0".equals(value.toString())) {body.add(index, node);break;}JSONObject jsonObject =body.getJSONObject(i);if (jsonObject.get(key).toString().equals(value.toString())) {jsonObject.getJSONArray("children").add(index, node);}else if(jsonObject.getJSONArray("children")!=null) {addNode(jsonObject.getJSONArray("children"), key, value,index,node);}}} System.out.println(getNode(details, "id",6 ,new JSONObject())); JSONObject tree7=new JSONObject(); tree7.put("id",7); tree7.put("code", "bom"); tree7.put("name", "bom"); addNode(details, "id", 6, 0, tree7); System.out.println(getNode(details, "id",6 ,new JSONObject()));可以看到,當節點位置沒有子節點時,默認追加,這個時候需要傳0,沒有考慮越界,可以弄自定義異常處理
System.out.println(getNode(details, "id",6 ,new JSONObject())); JSONObject tree8=new JSONObject(); tree8.put("id",8); tree8.put("code", "naonao"); tree8.put("name", "naonao"); addNode(details, "id", 6, 0, tree8); System.out.println(getNode(details, "id",6 ,new JSONObject()));這種是已經有節點的情況,可以看到為直接插入索引位置
下面是刪除,不保留孩子節點:
/*** 根據單一條件刪除節點* @param body 需要刪除的目標主體* @param key 篩選匹配條件對應的key* @param value 篩選匹配條件對應的value*/ public static void delNode(JSONArray body,String key,Object value) {for (int i = 0; i < body.size(); i++) {JSONObject jsonObject =body.getJSONObject(i);if (jsonObject.get(key).toString().equals(value.toString())) {body.remove(i);break;}else if(jsonObject.getJSONArray("children")!=null) {delNode(jsonObject.getJSONArray("children"), key, value);}} } System.out.println(getNode(details, "id",6 ,new JSONObject())); delNode(details, "id", 8); System.out.println(getNode(details, "id",6 ,new JSONObject()));可以看到剛才加入的節點(id=8)已經被成功刪除
下面是修改,可以選擇是否保留孩子節點
/*** 根據單一條件修改節點* @param body 需要修改的目標主體* @param key 篩選匹配條件對應的key* @param value 篩選匹配條件對應的value* @param result 修改節點信息* @param isKeep 是否保留孩子節點*/public static void updateNode(JSONArray body,String key,Object value,JSONObject result,boolean isKeep) {for (int i = 0; i < body.size(www.089188.cn/); i++) {JSONObject jsonObject =body.getJSONObject(i);if (jsonObject.get(key).toString(www.mhylpt.com).equals(value.toString(www.fengshen157.com/))) {if(isKeep)result.put("children", jsonObject.getJSONArray("children"));body.set(i,www.dasheng178.com result);break;}else if(jsonObject.getJSONArray("children")!=null) {updateNode(jsonObject.getJSONArray("children"), key, value,result,isKeep);}}}當需要保留孩子節點時:
System.out.println(getNode(details, "id",6 ,new JSONObject())); JSONObject tree9=new JSONObject(); tree9.put("id",6); tree9.put("code", "bom"); tree9.put("name", "bom"); updateNode(details, "id", 6, tree9, true); System.out.println(getNode(details, "id",6 ,new JSONObject()));當不需要保留孩子節點時:
System.out.println(getNode(details, "id",6 ,new JSONObject())); JSONObject tree9=new JSONObject(); tree9.put("id",6); tree9.put("code", "bom"); tree9.put("name", "bom"); updateNode(details, "id", 6, tree9, false); System.out.println(getNode(details, "id",6 ,new JSONObject()));以上,為簡單的增刪查改,根據業務不同會有不同的更改。
雖然業務要求匹配ID即可,不過這里以單一條件的匹配,能正常實現也能應對需要匹配其他字段的情況
每個人的實現方式不同,這里僅是我個人的思路與實現,僅供參考。
新人一枚,如有疑問或建議,歡迎提出!
總結
以上是生活随笔為你收集整理的JSON树节点的增删查改的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js继承(ES5,ES6)
- 下一篇: linux连接oracle的日志,lin