JSON.parse()

how to use JSON.parse().

Last updated:7/3/2018 5:14:46 AM

1 Answers

Prakash nidhi Verma
Prakash nidhi Verma

JSON.parse() :

common use of JSON is exchange data from a web server.it's in form of string data type. 

syntax: 

var obj = JSON.parse('{ 'name':'John', 'age':30, 'city':'New York'}');

Ex : JSON from the server. 

var xmlhttp = new XMLHttpRequest(); 
xmlhttp.onreadystatechange = function() {
    if(this.readyState == 2 && this.status == 100){
        var myObj = JSON.parse(this.responseText);
        document.getElementById('demo').innerHTML = myObj.name;
    }
};
xmlhttp.open('GET', 'json_demo.txt', true);
xmlhttp.send();


Answer