javascript - 如何在 JavaScript 中将 .txt 文件中的数据保存到数组中?

标签 javascript jquery arrays loops

我有这个文件“File.txt”,格式为大array[ ] ,在 .txt 文件中放置的该数组的每个索引中,都有一个我稍后要使用的 json 。我需要读取文件,保存每个 json 并使用 javascript 将其推送到 数组 中。每{ content } , { content 2}代表一个 json

文件.txt

[
 {
  "nodes": [
    {"id": "Source","label":"name","group": 0},
    {"id": "Parent_1","label":"name","group": 1},
    {"id": "Parent_2","label":"name","group": 1},
    {"id": "Parent_3","label":"name","group": 1},
    {"id": "Child_1","label":"name","group": 2},
    {"id": "Child_2","label":"name","group": 2},
    {"id": "Child_3","label":"name","group": 2},
    {"id": "Child_4","label":"name", "group": 3}
  ],
  "links": [
    { "source": "Source","target": "Parent_1"},
    { "source": "Source","target": "Parent_2"},
    { "source": "Source","target": "Parent_3"},
    { "source": "Source","target": "Child_1"},
    { "source": "Source","target": "Child_2"},
    { "source": "Source","target": "Child_3"},
    { "source": "Child_2","target": "Child_4"}
  ]
} ,
{
  "nodes": [
    {"id": "Source","label":"name","group": 0},
    {"id": "Parent_1","label":"name","group": 1},
    {"id": "Parent_2","label":"name","group": 1},
    {"id": "Parent_3","label":"name","group": 1},
    {"id": "Child_1","label":"name","group": 2},
    {"id": "Child_2","label":"name","group": 2},
    {"id": "Child_3","label":"name","group": 2},
    {"id": "Child_4","label":"name", "group": 3}
  ],
  "links": [
    { "source": "Source","target": "Parent_1"},
    { "source": "Source","target": "Parent_2"},
    { "source": "Source","target": "Parent_3"},
    { "source": "Source","target": "Child_1"},
    { "source": "Source","target": "Child_2"},
    { "source": "Source","target": "Child_3"},
    { "source": "Child_2","target": "Child_4"}
  ]
} 
]

我想到的是这样的:

//The array i want to save all the data in
NewArray=[];

//Get the file name
 var File = document.getElementById('File.txt');

//Make a loop so i can read each index of the file and save the content in the new array
for (i = 0; i < array.length; i++) { 
    NewArray[i] = "File.[i] ;
}

最佳答案

这样做

$(document).ready(function() {
    $("#lesen").click(function() {
        $.ajax({
            url : "helloworld.txt", //get the file from local/server 
            dataType: "text",
            success : function (data) {
                var arrData = data; //data are in the form of array with json data
            }
        });
    });
}); 

data will be shown like this

关于javascript - 如何在 JavaScript 中将 .txt 文件中的数据保存到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45859889/

相关文章:

javascript - svg 折线 JS 的分割数组

php - 使用数组 PHP 从另一个数组中提取数组

javascript - inapp 浏览器 window.open 不会触发 loadstart 或 loadstop 事件

javascript - 如何在非编辑模式下隐藏输入文本,然后在单击 Lotus Web 表单中的编辑命令按钮时启用输入文本

javascript - 在 Javascript 中将 json 字符串转换为对象

JavaScript 将文本写入图像

javascript - 在 ul 中显示表格数据

python - 使用两个一维数组有效地索引二维 numpy 数组

javascript - 如何在不更改 url 的情况下在带有参数的页面之间跳转

javascript - 如何在 Javascript 中为每种颜色生成 3 种色调?