簡體   English   中英

修改 Json 數據以通過 JavaScript 創建新對象

[英]Modify Json data to create new object by JavaScript

我有一個json數據如下

{
  "file1": {
    "function1": {
      "calls": {
        "439:0": [
          "441:24"
        ],
        "441:24": [
          "443:4"
        ],
        "443:4": [
          "447:7",
          "445:10"
        ],
        "445:10": [
          "449:4"
        ],
        "447:7": [
          "449:4"
        ]
      }
    },
    "function2": {
      "calls": {
        "391:0": [
          "393:8"
        ],
        "393:8": [
          "397:7"
        ],
        "397:7": [
          "395:27"
        ]
      }
    },
    "function3": {
      "calls": null
    },
    "function4": {
      "calls": null
    }
  },
  "file2": {
    "function5": {
      "calls": null
    },
    "function6": {
      "calls": {
        "391:0": [
          "393:8"
        ],
        "393:8": [
          "397:7"
        ],
        "397:7": [
          "395:27"
        ]
      }
    }
  }
}

我需要按照“function1”的格式轉換它

    {
  "nodes": [
    {
      "id": "439:0",
      "line": "439:0"
    },
    {
      "id": "441:24",
      "line": "441:24"
    },
    {
      "id": "443:4",
      "line": "443:4"
    },
    {
      "id": "447:7",
      "line": "447:7"
    },
    {
      "id": "445:10",
      "line": "445:10"
    },
    {
      "id": "449:4",
      "line": "449:4"
    }
  ],
  "links": [
    {
      "source": "439:0",
      "target": "441:24"
    },
    {
      "source": "441:24",
      "target": "443:4"
    },
    {
      "source": "443:4",
      "target": "447:7"
    },
    {
      "source": "443:4",
      "target": "445:10"
    },
    {
      "source": "445:10",
      "target": "449:4"
    },
    {
      "source": "447:7",
      "target": "449:4"
    }
  ]
}

其中“調用”鍵是“節點”中的行和 ID,“鏈接”中的源。 目標是調用中的值。 如果任何鍵有多個值而不是每個值,它將創建一個源-目標對。

這將在稍后用作data.nodes.map(function(d){return d.line})

我嘗試了以下代碼,但無法正常工作。 它提供了一個包含正確信息的數組,但我需要在進一步的步驟中使用它的方式,它在那里不起作用。 它給出的錯誤如Property 'nodes' does not exist on type 'any[]'

let res = {}
let nodes = []
let links = []
Object.entries(input0).map(([fileName, fileObject]) => {
  Object.entries(fileObject).map(([functionName, functionObject]) => {
    if(functionName=="function1"){
      Object.entries(functionObject).map(([functionKey, functionValue]) => { 
        if(functionKey === "calls") {
          if(functionValue != null){
            Object.entries(functionValue).map(([callKey, callObject]) => {     
              nodes = [...nodes,{"id": callKey, "line": callKey}] 
              callObject.forEach(x => {
                links = [...links,{"source": callKey, "target": x}] 
              });
              res = {"nodes": nodes, "links": links} //nodes.concat(links)
            })                     
          }
        }
      })
    }
  })
})
console.log(res)

有人請幫忙。 感謝您的時間。

您可以省略不需要的屬性並直接嵌套一次,然后通過檢查現有節點並分配所有鏈接來構建一組新節點,而無需進一步檢查。

 function getNodesLinks(object, result = { nodes: [], links: [] }) { if (object.calls === null) return result; if (!object.calls) { Object.values(object).forEach(v => getNodesLinks(v, result)); return result; } Object.entries(object.calls).forEach(([source, targets]) => { if (!result.nodes.some(({ id }) => id === source)) { result.nodes.push({ id: source, line: source }); } targets.forEach(target => { if (!result.nodes.some(({ id }) => id === target)) { result.nodes.push({ id: target, line: target }); } result.links.push({ source, target }); }); }); return result; } var data = { file1: { function1: { calls: { "439:0": ["441:24"], "441:24": ["443:4"], "443:4": ["447:7", "445:10"], "445:10": ["449:4"], "447:7": ["449:4"] } }, function2: { calls: { "391:0": ["393:8"], "393:8": ["397:7"], "397:7": ["395:27"] } }, function3: { calls: null }, function4: { calls: null } }, file2: { function5: { calls: null }, function6: { calls: { "391:0": ["393:8"], "393:8": ["397:7"], "397:7": ["395:27"] } } } }, result = getNodesLinks(data); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

你可以試試這個,這應該可以正常工作。

           var res = {};
            var nodes = [];
            var Links = [];
            var json = '{"file1":{"function1":{"calls":{"439:0":["441:24"],"441:24":["443:4"],"443:4":["447:7","445:10"],"445:10":["449:4"],"447:7":["449:4"]}},"function2":{"calls":{"391:0":["393:8"],"393:8":["397:7"],"397:7":["395:27"]}},"function3":{"calls":null},"function4":{"calls":null}},"file2":{"function5":{"calls":null},"function6":{"calls":{"391:0":["393:8"],"393:8":["397:7"],"397:7":["395:27"]}}}}';
            var jsonObj = JSON.parse(json);
            if ("function1" in jsonObj.file1) {
                if ('calls' in jsonObj.file1.function1) {
                    for (var i = 0; Object.keys(jsonObj.file1.function1.calls).length > i; i++) {
                        nodes.push({ "id": Object.keys(jsonObj.file1.function1.calls)[i], "line": Object.keys(jsonObj.file1.function1.calls)[i] });
                            for (var j = 0; Object.values(jsonObj.file1.function1.calls)[i].length > j; j++) {
                                Links.push({ "source": Object.keys(jsonObj.file1.function1.calls)[i], "target": Object.values(jsonObj.file1.function1.calls)[i][j] });
                        }
                    }
                    res = { "nodes": nodes, "links": Links }
                    console.log(res);
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM