javascript
JSON树节点的增删查改
最近了解到使用json字符串存到數(shù)據(jù)庫的一種存儲(chǔ)方式,取出來的json字符串可以進(jìn)行相應(yīng)的節(jié)點(diǎn)操作
故借此機(jī)會(huì)練習(xí)下遞歸,完成對(duì)json節(jié)點(diǎn)操作對(duì)應(yīng)的工具類。
介紹一下我使用的依賴
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.49</version> </dependency>主要使用JSONObject和JSONArray的API進(jìn)行相關(guān)操作,這里附上這兩個(gè)類的代碼
JSONObject
JSONArray
一些數(shù)據(jù)轉(zhuǎn)換的API
JSONArray.parseArray(“json樹字符串”)-----------可以返回JSONArray對(duì)象
jsonArray對(duì)象.toJSONString()------------------------可以轉(zhuǎn)換為字符串便于存入數(shù)據(jù)庫
首先我們需要有一個(gè)json樹,這里可以自己編寫,跟數(shù)據(jù)庫操作的相關(guān)方法暫不涉及,這里直接使用相關(guān)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樹
點(diǎn)擊上面可以查看生成的json樹
OK,準(zhǔn)備工作完畢,下面進(jìn)行功能演示。
(注意,每演示一個(gè)功能點(diǎn),請(qǐng)注釋掉其他的打印語句)
首先是查詢:
/*** 根據(jù)單一條件獲取節(jié)點(diǎn)位置* @param body 查詢目標(biāo)的主體內(nèi)容* @param key 篩選匹配條件條件對(duì)應(yīng)--key* @param value 篩選匹配條件對(duì)應(yīng)--value* @param result 緩存查詢結(jié)果* @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方法調(diào)用演示,將前面的打印注釋掉
//System.out.println(details); System.out.println(getNode(details, "id", 4,new JSONObject()));查詢寫出來,基本思路對(duì)了,其他的操作都類似,簡(jiǎn)單得多
下面是添加
/*** * @param body 需要添加的目標(biāo)樹主體* @param key 篩選匹配條件對(duì)應(yīng)的key* @param value 篩選匹配條件的值* @param index 需要插入的下標(biāo)位置索引* @param node 插入的整體節(jié)點(diǎn)*/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()));可以看到,當(dāng)節(jié)點(diǎn)位置沒有子節(jié)點(diǎn)時(shí),默認(rèn)追加,這個(gè)時(shí)候需要傳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()));這種是已經(jīng)有節(jié)點(diǎn)的情況,可以看到為直接插入索引位置
下面是刪除,不保留孩子節(jié)點(diǎn):
/*** 根據(jù)單一條件刪除節(jié)點(diǎn)* @param body 需要?jiǎng)h除的目標(biāo)主體* @param key 篩選匹配條件對(duì)應(yīng)的key* @param value 篩選匹配條件對(duì)應(yīng)的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()));可以看到剛才加入的節(jié)點(diǎn)(id=8)已經(jīng)被成功刪除
下面是修改,可以選擇是否保留孩子節(jié)點(diǎn)
/*** 根據(jù)單一條件修改節(jié)點(diǎn)* @param body 需要修改的目標(biāo)主體* @param key 篩選匹配條件對(duì)應(yīng)的key* @param value 篩選匹配條件對(duì)應(yīng)的value* @param result 修改節(jié)點(diǎn)信息* @param isKeep 是否保留孩子節(jié)點(diǎn)*/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);}}}當(dāng)需要保留孩子節(jié)點(diǎn)時(shí):
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()));當(dāng)不需要保留孩子節(jié)點(diǎn)時(shí):
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()));以上,為簡(jiǎn)單的增刪查改,根據(jù)業(yè)務(wù)不同會(huì)有不同的更改。
雖然業(yè)務(wù)要求匹配ID即可,不過這里以單一條件的匹配,能正常實(shí)現(xiàn)也能應(yīng)對(duì)需要匹配其他字段的情況
每個(gè)人的實(shí)現(xiàn)方式不同,這里僅是我個(gè)人的思路與實(shí)現(xiàn),僅供參考。
新人一枚,如有疑問或建議,歡迎提出!
總結(jié)
以上是生活随笔為你收集整理的JSON树节点的增删查改的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js继承(ES5,ES6)
- 下一篇: JS如何取得URL里的参数?