Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将文本文件转换为JSON_Javascript_Json_File Io - Fatal编程技术网

Javascript 将文本文件转换为JSON

Javascript 将文本文件转换为JSON,javascript,json,file-io,Javascript,Json,File Io,我正在尝试编写一个函数,将文本文件作为输入,并将其转换为下面的JSON树格式,以便在我的d3.js项目中使用它 文本文件非常简单:以“b”开头的每一行代表一个包,下面的整数是包的编号。每个包都包含节点 因此,第一行是带有节点1和节点2的包1。 不包含b的线表示行李之间的链接。例如,包1指向包2 样本输入: b 1 1 2 3 b 2 2 3 4 b 3 4 5 6 1 2 1 3 预期产出: const tree = { id: 1, name: '1, 2, 3', vertic

我正在尝试编写一个函数,将文本文件作为输入,并将其转换为下面的JSON树格式,以便在我的d3.js项目中使用它

文本文件非常简单:以“b”开头的每一行代表一个包,下面的整数是包的编号。每个包都包含节点

因此,第一行是带有节点1和节点2的包1。 不包含b的线表示行李之间的链接。例如,包1指向包2

样本输入:

b 1 1 2 3
b 2 2 3 4
b 3 4 5 6
1 2
1 3
预期产出:

const tree = {
  id: 1,
  name: '1, 2, 3',
  vertices: [1, 2, 3],
  children: [
    {
      id: 2,
      name: '2, 3, 4',
      vertices: [2, 3, 4],
    },
    {
      id: 3,
      name: '4, 5, 6',
      vertices: [4, 5, 6],
    },
  ],
};
到目前为止的代码(tom提供的帮助):

函数readTreeInput(evt){
const file=evt.target.files[0];
const fileReader=new fileReader();
fileReader.onload=函数convertTreeToJSON(){
常量行=this.result.split('\n');
常数res={};让电流=res;
for(让line=0;line
不太清楚如何从这里开始照顾筑巢,有什么建议吗?:)

有什么问题吗?;-)我更喜欢更简单的textLine.split(“”),但保留了大部分代码不变。假设FileReader在这里不起作用

var result=document.createElement('PRE');
result.innerText=x=JSON.stringify(
convertTreeToJSON.call({result:'b1 1 2\nb 2 2 3\nb 3 4 3\nb 4 5 4\n1 2\n2 3\n3 4'})
,null,1)
。替换(/\[\n\s+(\d+)\n\s+(\d+)\n\s+]/g,[$1,$2]'))
.replace(/\[\n\s+/g,'[')。replace(/}\n\s+\]/g,'}]');
document.body.appendChild(结果);
函数convertTreeToJSON(x){
常量行=this.result.split('\n');
常数边=[];
常量treeBags=[];
常数listOfIds=[];
var res={};var current;
for(让line=0;line}
谢谢。我唯一想改变的是,如果一个节点没有子节点,我希望它像上面的输出一样:没有线或数组,所以它只是id:4,名称:'5,4',顶点:[5,4],有没有办法做到这一点?不用担心,睡一觉,感谢您的帮助:)文本文件中不以b开头的行决定了节点之间的链接。因此,bag 1链接到bag 2,bag 1也链接到bag 3,因此树看起来是这样的:在这种情况下,第一个节点将有两个孩子,您可以再次玩;-)
function readTreeInput(evt) {
  const file = evt.target.files[0];
  const fileReader = new FileReader();

  fileReader.onload = function convertTreeToJSON() {
    const lines = this.result.split('\n');
    const res = {}; let current = res;

    for (let line = 0; line < lines.length; line++) {
      const textLine = lines[line];
      if (textLine.startsWith('c') || textLine.startsWith('s')) continue;

      if (textLine.startsWith('b')) {
        const bagId = parseInt(textLine[2], 10);
        const firstNode = textLine[4];
        const secondNode = textLine[6];
        const thirdNode = textLine[8];
        let vertices;

        if (secondNode === undefined) {
          vertices = [firstNode];
        } else if (thirdNode === undefined) {
          vertices = [parseInt(firstNode, 10), parseInt(secondNode, 10)];
        } else {
          vertices = [firstNode, secondNode, thirdNode];
        }
        current.id = bagId;
        current.name = vertices.join(', '); // bagLabel;
        current.vertices = vertices;
        current = (current.children = [{}])[0];
      }
    }
    removeExistingTree();
    drawTree(res);
  };
  fileReader.readAsText(file);
}