欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用json字符串插入節(jié)點(diǎn)或者覆蓋節(jié)點(diǎn)

 更新時(shí)間:2021年08月10日 16:10:01   作者:BugCounter  
這篇文章主要介紹了使用json字符串插入節(jié)點(diǎn)或者覆蓋節(jié)點(diǎn)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

json字符串插入節(jié)點(diǎn)或者覆蓋節(jié)點(diǎn)

jfdaJson是json字符串

JSONObject obj=JSONObject.parseObject(jfdaJson);
obj.put("dj",xydjDm);// 更新dj字段
obj.put("xydjMc",xydjMc);// 添加xydjMc字段
obj.toString();

json字符串轉(zhuǎn)換成json增刪查改節(jié)點(diǎn)

一、功能實(shí)現(xiàn)

1、節(jié)點(diǎn)樹查詢:

按ID查詢樹

2、節(jié)點(diǎn)新增:

http://host/tree_data/node/${treeId}
in: {node: {key: ..., ...}, parent: ${key}, sequ: ${sequ}}

3、節(jié)點(diǎn)修改

http://host/tree_data/node/${treeId}/${key}
in: {node: {key: ..., ...}}

4、節(jié)點(diǎn)刪除

http://host/tree_data/node/${treeId}/${key}

二、數(shù)據(jù)表結(jié)構(gòu)

CREATE TABLE `catagory_tree`  (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '分類樹名稱:PRODUCT:產(chǎn)品分類  GOODS:商品分類',
  `json_tree` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '直接存儲(chǔ)ANTD樹表的數(shù)據(jù)結(jié)構(gòu)',
  `modify_staff` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `modify_time` datetime(0) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `AK_uni_catagory_tree_name`(`name`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

三、json字符串

[{
 "id": 1,
 "code": "FLOW_NODE_1",
 "name": "環(huán)節(jié)A",
 "children": [{
  "id": 2,
  "code": "RULE_NODE_1",
  "name": "規(guī)則A"
 }, {
  "id": 3,
  "code": "RULE_NODE_2",
  "name": "規(guī)則B"
 }, {
  "id": 4,
  "code": "PARALLEL_NODE_2",
  "name": "并行節(jié)點(diǎn)",
  "children": [{
   "id": 5,
   "code": "RULE_NODE_3",
   "name": "規(guī)則C1"
  }, {
   "id": 6,
   "code": "RULE_COLLECTION_1",
   "name": "規(guī)則集1",
   "children": [{
    "id": 7,
    "code": "RULE_NODE_4",
    "name": "規(guī)則C21"
   }, {
    "id": 8,
    "code": "RULE_NODE_5",
    "name": "規(guī)則C22"
   }]
  }]
 }, {
  "id": 9,
  "code": "MUTUAL_NODE_1",
  "name": "互斥節(jié)點(diǎn)",
  "children": [{
   "id": 10,
   "code": "RULE_NODE_6",
   "name": "規(guī)則D1"
  }, {
   "id": 11,
   "code": "RULE_NODE_7",
   "name": "規(guī)則D2"
  }]
 }]
}, {
 "id": 12,
 "code": "FLOW_NODE_2",
 "name": "環(huán)節(jié)B"
}]

四、pom文件依賴

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.49</version>
</dependency>

五、controller層

package cn.chinaunicom.srigz.goods.admin.controller;
import cn.chinaunicom.srigz.goods.admin.service.CatagoryTreeService;
import cn.chinaunicom.srigz.goods.admin.utils.ActionHelper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class CatagoryTreeController {
    @Autowired
    private CatagoryTreeService catagoryTreeService;
    @ApiOperation(value="查詢分類樹信息", notes="輸入分類樹Id")
    @RequestMapping(value = "/api/v1/tree_data/node/{treeId}", method = RequestMethod.GET)
    public Map getOne(@ApiParam(required = true, value = "分類樹ID") @PathVariable Integer treeId){
        return ActionHelper.responseOk(catagoryTreeService.getOne(treeId));
    }
    @ApiOperation(value="新建節(jié)點(diǎn)", notes="根據(jù)輸入?yún)?shù)創(chuàng)建節(jié)點(diǎn)")
    @RequestMapping(value = "/tree_data/node/{treeId}", method = RequestMethod.POST)
    public Map addOne(@ApiParam(required = true, value = "分類樹ID")@PathVariable Integer treeId,
                      @ApiParam(required = true, value = "節(jié)點(diǎn)信息、父類ID、數(shù)組位置") @RequestBody Map map){
        return ActionHelper.responseOk(catagoryTreeService.addOne(treeId,map));
    }
    @ApiOperation(value="更新節(jié)點(diǎn)信息", notes="更新節(jié)點(diǎn)信息")
    @RequestMapping(value = "/tree_data/node/{treeId}/{id}", method = RequestMethod.PATCH)
    public Map updateOne(@ApiParam(required = true, value = "分類樹ID")@PathVariable Integer treeId,
                         @ApiParam(required = true, value = "節(jié)點(diǎn)ID")@PathVariable Integer id,
                         @ApiParam(required = true, value = "節(jié)點(diǎn)信息") @RequestBody Map map){
        return ActionHelper.responseOk(catagoryTreeService.updateOne(treeId,id,map));
    }
    @ApiOperation(value="刪除節(jié)點(diǎn)詳情", notes="刪除節(jié)點(diǎn)詳情")
    @RequestMapping(method = RequestMethod.DELETE,value = "/tree_data/node/{treeId}/{id}")
    public Map remove(@ApiParam(required = true, value = "分類樹ID")@PathVariable Integer treeId,
                      @ApiParam(required = true, value = "節(jié)點(diǎn)ID")@PathVariable Integer id){
        return ActionHelper.responseOk(catagoryTreeService.remove(treeId,id));
    }
}

六、service層

JSONArray jsonArray = JSON.parseArray(jsonTree); //由json字符串變成json數(shù)組對(duì)象
jsonArray.toJSONString() //由json數(shù)組對(duì)象變成json字符串
package cn.chinaunicom.srigz.goods.admin.service;
import cn.chinaunicom.srigz.goods.admin.database.dao.CatagoryTreeRepository;
import cn.chinaunicom.srigz.goods.admin.database.model.CatagoryTree;
import cn.chinaunicom.srigz.goods.admin.utils.Recursive;
import cn.chinaunicom.srigz.goods.common.exception.AlreadyExistException;
import cn.chinaunicom.srigz.goods.common.exception.BadRequestException;
import cn.chinaunicom.srigz.goods.common.exception.NotFoundException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@Service
public class CatagoryTreeService {
    @Autowired
    private CatagoryTreeRepository catagoryTreeRepository;
    /**
     * 查詢單個(gè)分類樹
     * @param treeId
     * @return
     */
    public JSON getOne(Integer treeId) {
        CatagoryTree catagoryTree = catagoryTreeRepository.findById(treeId.longValue()).get();
        return JSON.parseArray(catagoryTree.getJsonTree());
    }
    /**
     * 增加單個(gè)節(jié)點(diǎn)
     * @param treeId
     * @param map
     * @return
     */
    @Transactional
    public JSON addOne(Integer treeId, Map map) {
        CatagoryTree catagoryTree = catagoryTreeRepository.findById(treeId.longValue()).get();
        String jsonTree = catagoryTree.getJsonTree();
        JSONArray jsonArray = JSON.parseArray(jsonTree);
        Object parentId = map.get("parentId");
        Integer sequ = (Integer) map.get("sequ");
        if(sequ <= 0 ){
            throw new BadRequestException("數(shù)組位置不正確");
        }
        Map nodeMap = (Map) map.get("node");
        JSONObject node = new JSONObject(nodeMap);
        List list = new ArrayList();
        List keyList = Recursive.recursive(jsonArray,"id",list);
        for (Object key : keyList) {
            if (nodeMap.get("id").toString().equals(key.toString())) {
                throw new AlreadyExistException("節(jié)點(diǎn)ID為" + key);
            }
        }
        Recursive.addRecursive(jsonArray,node,sequ,"id",parentId);
        catagoryTree.setJsonTree(jsonArray.toJSONString());
        catagoryTreeRepository.save(catagoryTree);
        return jsonArray;
    }
    /**
     * 修改單個(gè)節(jié)點(diǎn)
     * @param treeId
     * @param id
     * @param map
     * @return
     */
    @Transactional
    public JSON updateOne(Integer treeId, Integer id, Map map) {
        CatagoryTree catagoryTree = catagoryTreeRepository.findById(treeId.longValue()).get();
        String jsonTree = catagoryTree.getJsonTree();
        JSONArray jsonArray = JSON.parseArray(jsonTree);
        List list = new ArrayList();
        List keyList = Recursive.recursive(jsonArray,"id",list);
        JSONObject node = new JSONObject(map);
        if (id.toString().equals(map.get("id").toString())) {
            for (Object key : keyList) {
                if (id.toString().equals(key.toString())) {
                    Recursive.updateRecursive(jsonArray,"id",id,node);
                    catagoryTree.setJsonTree(jsonArray.toJSONString());
                    catagoryTree.setModifyTime(new Date());
                    catagoryTreeRepository.save(catagoryTree);
                    return jsonArray;
                }
            }
        }else {
            throw new BadRequestException("id is not allowed to be modified");
        }
        throw new NotFoundException("節(jié)點(diǎn)ID為" + id);
    }
    /**
     * 刪除節(jié)點(diǎn)以及子節(jié)點(diǎn)
     * @param treeId
     * @param id
     * @return
     */
    @Transactional
    public JSON remove(Integer treeId, Integer id) {
        CatagoryTree catagoryTree = catagoryTreeRepository.findById(treeId.longValue()).get();
        String jsonTree = catagoryTree.getJsonTree();
        JSONArray jsonArray = JSON.parseArray(jsonTree);
        List list = new ArrayList();
        List keyList = Recursive.recursive(jsonArray,"id",list);
        for (Object key : keyList) {
            if (id.toString().equals(key.toString())) {
                Recursive.removeRecursive(jsonArray,"id",id);
                catagoryTree.setJsonTree(jsonArray.toJSONString());
                catagoryTreeRepository.save(catagoryTree);
                return jsonArray;
            }
        }
        throw new NotFoundException("節(jié)點(diǎn)ID為" + id);
    }
}

七、把增刪查改的遞歸方法寫成工具類

package cn.chinaunicom.srigz.goods.admin.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
public class Recursive {
    /**
     * 遞歸取出所有Key對(duì)應(yīng)的value值
     * @param jsonArray     需要查詢的目標(biāo)主體
     * @param key           需要查詢的字段名key
     * @param list          存儲(chǔ)value值
     * @return
     */
    public static List recursive(JSONArray jsonArray, String key, List list) {
        for (Object obj : jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            JSONArray children = jsonObject.getJSONArray("children");
            if (children != null) {
                list.add(jsonObject.get(key));
                recursive(children,key,list);
            }else {
                list.add(jsonObject.get(key));
            }
        }
        return list;
    }
    /**
     * 增加節(jié)點(diǎn)
     * @param jsonArray     需要更新的目標(biāo)主體
     * @param node          增加節(jié)點(diǎn)信息
     * @param sequ          數(shù)組的位置
     * @param key           目標(biāo)主體中的字段名key
     * @param value         節(jié)點(diǎn)信息與key對(duì)應(yīng)的value
     */
    public static void addRecursive(JSONArray jsonArray, JSONObject node, Integer sequ, String key, Object value) {
        for (Object obj : jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            JSONArray children = jsonObject.getJSONArray("children");
            if (children != null) {
                if (value.toString().equals(jsonObject.get(key).toString())) {
                    if(sequ > children.size()){
                        children.add(children.size(), node);
                    }else{
                        children.add(sequ-1, node);
                    }
                    return;
                }
                addRecursive(children,node,sequ,key,value);
            } else {
                if (value.toString().equals(jsonObject.get(key).toString())) {
                    JSONArray array = new JSONArray();
                    array.add(node);
                    jsonObject.put("children", array);
                    return;
                }
            }
        }
    }
    /**
     * 根據(jù)條件更新節(jié)點(diǎn)
     * @param jsonArray 需要更新的目標(biāo)主體
     * @param key       目標(biāo)主體中的字段名key
     * @param value     更新節(jié)點(diǎn)信息與key對(duì)應(yīng)的value
     * @param node      更新節(jié)點(diǎn)信息
     */
    public static void updateRecursive(JSONArray jsonArray, String key, Object value, JSONObject node) {
        for (int i = 0; i < jsonArray.size() ; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONArray children = jsonObject.getJSONArray("children");
            if (value.toString().equals(jsonObject.get(key).toString())) {
                if(children != null){
                    node.put("children",children);
                }
                jsonArray.set(i,node);
                return;
            }else if(children != null){
                updateRecursive(children,key,value,node);
            }
        }
    }
    /**
     * 刪除節(jié)點(diǎn)
     * @param jsonArray     需要更新的目標(biāo)主體
     * @param key           目標(biāo)主體中的字段名key
     * @param value         節(jié)點(diǎn)信息與key對(duì)應(yīng)的value
     */
    public static void removeRecursive(JSONArray jsonArray,String key,Object value) {
        for (Object obj : jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            if (value.toString().equals(jsonObject.get(key).toString())) {
                jsonArray.remove(jsonObject);
                return;
            }
            JSONArray children = jsonObject.getJSONArray("children");
            if (children != null) {
                removeRecursive(children,key,value);
            }
        }
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁面

    springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁面

    Spring?Security是一種基于Spring框架的安全技術(shù),用于實(shí)現(xiàn)身份驗(yàn)證和訪問控制,本文介紹了如何使用Spring?Security,結(jié)合session和redis來存儲(chǔ)用戶信息,并通過編寫特定的登錄處理類和Web配置,實(shí)現(xiàn)用戶登錄和注銷功能
    2024-09-09
  • Java上傳下載文件并實(shí)現(xiàn)加密解密

    Java上傳下載文件并實(shí)現(xiàn)加密解密

    這篇文章主要介紹了Java上傳下載文件并實(shí)現(xiàn)加密解密,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Mybatis choose when用法實(shí)例代碼

    Mybatis choose when用法實(shí)例代碼

    本文通過實(shí)例代碼給大家介紹了Mybatis choose when用法,需要的的朋友參考下吧
    2017-06-06
  • mybatis插入與批量插入返回ID的原理詳解

    mybatis插入與批量插入返回ID的原理詳解

    這篇文章主要給大家介紹了關(guān)于mybatis插入與批量插入返回ID的原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Java?Spring?boot?配置JDK和MAVEN開發(fā)環(huán)境的過程

    Java?Spring?boot?配置JDK和MAVEN開發(fā)環(huán)境的過程

    本文詳細(xì)介紹了如何配置JDK和Maven環(huán)境,包括JDK的安裝與環(huán)境變量設(shè)置,Maven的下載、配置環(huán)境變量和設(shè)置阿里云倉庫,最后簡述了在IntelliJ?IDEA中配置JDK和Maven的步驟,本教程適合Java開發(fā)新手進(jìn)行開發(fā)環(huán)境的搭建,確保順利進(jìn)行Java項(xiàng)目的開發(fā)
    2024-11-11
  • Spring Boot 2.2 正式發(fā)布,大幅性能提升 + Java 13 支持

    Spring Boot 2.2 正式發(fā)布,大幅性能提升 + Java 13 支持

    隨著 Spring Framework 5.2.0 成功發(fā)布之后,Spring Boot 2.2 也緊跟其后,發(fā)布了第一個(gè)版本:2.2.0。下面就來一起來看看這個(gè)版本都更新了些什么值得我們關(guān)注的內(nèi)容
    2019-10-10
  • Spring的IOC解決程序耦合的實(shí)現(xiàn)

    Spring的IOC解決程序耦合的實(shí)現(xiàn)

    本文主要介紹了Spring的IOC解決程序耦合的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • java學(xué)習(xí)之JVM運(yùn)行時(shí)常量池理解

    java學(xué)習(xí)之JVM運(yùn)行時(shí)常量池理解

    這篇文章主要介紹了java學(xué)習(xí)之JVM運(yùn)行時(shí)常量池理解,對(duì)常量池的好處以及基本類型的包裝類常量池等作了簡要分析,有需要的朋友可以借鑒參考下
    2021-09-09
  • Java動(dòng)態(tài)腳本Groovy獲取Bean技巧

    Java動(dòng)態(tài)腳本Groovy獲取Bean技巧

    這篇文章主要給大家分享的是Java動(dòng)態(tài)腳本Groovy獲取Bean技巧,在Java代碼中當(dāng)我們需要一個(gè)Bean對(duì)象,通常會(huì)使用spring中@Autowired注解,用來自動(dòng)裝配對(duì)象。下面我們一起進(jìn)入文章學(xué)習(xí)個(gè)表格多 詳細(xì)內(nèi)容吧

    2021-12-12
  • Java Swing SpringLayout彈性布局的實(shí)現(xiàn)代碼

    Java Swing SpringLayout彈性布局的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java Swing SpringLayout彈性布局的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12

最新評(píng)論