how to get json data from a file using javascript


 
 
 
 
 
 
 
 

how to get json data from a file using javascript

<html>
<title></title>
<head>
<script>
var resultDiv = document.getElementById(“json-data”);
var newsURL = “http://localhost:sales/data.json”;
var xmlhttp;
if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        response = JSON.parse(xmlhttp.responseText);
        //we can manipulate the response as you wish, for example you can:
        var myData = response[‘JsonProjectIDResult’];
        // Data now is an array of the json object
        var html = ‘<ul>’;
        for(var prop in myData ) {
            if(myData.hasOwnProperty(prop))
                 html += ‘<li>’ + prop  + ‘ = ‘ + myData[prop] + ‘</li>’;
        }
        html += ‘</ul>’;
        //and so on
        resultDiv.innerHTML = html;
    }
}
xmlhttp.open(“GET”, newsURL, true);
xmlhttp.send();
</script></head>
<div id=”json -data”>
    <!-displaying json data—>
</div>
</body>
</html>