2017-08-24 2 views
0

Ich habe diese Datei, "File.txt" formatiert als eine große array[ ], in jedem Index dieses Arrays in der TXT-Datei befindet sich ein JSON Ich möchte später verwenden. Ich muss die Datei lesen, speichern Sie jeweils JSON und schieben Sie es in eine Array mit Javascript. Jede { content }, { content 2} repräsentiert eine json.Wie kann ich Daten aus einer TXT-Datei in ein Array in JavaScript speichern?

File.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"} 
    ] 
} 
] 

ich im Sinn so etwas wie dieses:

//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] ; 
} 
+0

JavaScript kann nur lokale Dateien lesen. Ich nehme an, Ihre Textdatei befindet sich auf dem Server? – hallleron

+0

@hallleron Es ist lokal. –

+1

Bearbeiten Sie dann Ihre Datei, setzen Sie ihren Inhalt mit 'var data =' voran und laden Sie sie dann über ein '' Element. – trincot

Antwort

1

Eine Möglichkeit, es Arbeit zu machen, ist die Textdatei zu bearbeiten, und das Präfix sein Inhalt mit

var data = 

laden Dann ist es über:

<script src="file.txt"></script> 

Sobald Sie, dass Sie auf die Daten zugreifen können über die data Variable.

2

es gefällt das

$(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

+0

Ich legte Ihre Antwort in