开发者

deleting just the parent node and not the child node in json object

{"0":
     {"id":"1",
      "Name":"A1",
      "parent_id":"0"},
 "1":
    {"id":"2",
     "Name":"A2",
     "parent_id":"0",
     "0":
         {"id":"3",
          "Name":"A3",
          "parent_id":"2",
          "0": 
              {"id":"4",
               "Name":"A4",
             开发者_C百科  "parent_id":"3"}}}}

This is populated from a database table. I want to delete only node id 2(A2). Id 3's parent id should automatically become id 1. That is A2's immediate child becomes the child of it's parent along with its children. I cannot use splice or remove on this as I am using js with QML. Any suggestions? I am currently able to delete the entire node along with its children(a2,a3,a4) by finding the node that has to be deleted and assigning it to null.I am having a difficulty in deleting just the selected node and not its children.


jsonObject['1'] = jsonObject['1']['0'];

This should decrement the ref count for A2 and kill it but increment the ref count for A3, saving A3 and its children.


You can't simply delete. You need to copy the content you want to keep before deleting.

Maybe your real problem is the structure of the data itself. Most likely, you want the json structure to be flat, then use a function to recreate the tree as needed.


I've had to do something similar to this and unfortunately, the only way I found to do it was to rebuild the JSON structure manually. :(

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜