Pass Json data to Javascript

0

How can I extract data from json to javascript without using this as this example:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var myArr = JSON.parse(this.responseText);
        document.getElementById("demo").innerHTML = myArr[0];
    }
};
xmlhttp.open("GET", "json_demo_array.txt", true);
xmlhttp.send();
    
asked by Mary Katty Vicuña Ore 11.06.2018 в 01:14
source

1 answer

0

Your question is wrongly formulated but to collect data from a JSON with javascriptel code that you have is correct, if you want to consult then that data the best that you keep the JSON in an array I show you an example.

var datos = []
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var myArr = JSON.parse(this.responseText);
        document.getElementById("demo").innerHTML = myArr[0];
        for (let i = 0; i < myArr.length; i++) {
            datos.push(myArr[i])
        }
    }
};
xmlhttp.open("GET", "json_demo_array.txt", true);
xmlhttp.send();
    
answered by 11.06.2018 / 08:34
source