0

I have this file , " File.txt " formatted as a big array[ ] , in each index of this array placed in the .txt file there is a json i want to use later. I need to read the file , save each json and push it in an array using javascript. Every { content } , { content 2} represents a json .

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

I have in mind something like this :

//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] ;
}
4
  • JavaScript can only read local files. I guess your text file is on the server? Commented Aug 24, 2017 at 11:02
  • @hallleron It is local . Commented Aug 24, 2017 at 11:05
  • 1
    Then edit your file, prefix its contents with var data = , and then load it via a <script src="file.txt"></script> element. Commented Aug 24, 2017 at 11:08
  • @trincot I did exactly as you said , and it works i even tested it with this for(var i = 0; i < array.length; i++){ console.log(i + " = " + array[i]); } sorry i don't know how mark the text as a code in comments Commented Aug 24, 2017 at 11:46

2 Answers 2

3

Do it like this

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

Sign up to request clarification or add additional context in comments.

3 Comments

I put your answer in <script> </script> tags , It should work but it gives me this error : Uncaught ReferenceError: $ is not defined ;
this means you need to load jquery.
you need to add jquery script
1

One way to make it work is to edit your text file, and prefix its contents with

var data = 

Then load it via:

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

Once you have that, you can access the data via the data variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.