I am trying to parse JSON, but it gives the wrong result.
var obj = JSON.parse('[{ "name":"John", "age":30, "city":"New York"}]');
alert(obj);
Please check the code and provide me any suggestion.
I am trying to parse JSON, but it gives the wrong result.
var obj = JSON.parse('[{ "name":"John", "age":30, "city":"New York"}]');
alert(obj);
Please check the code and provide me any suggestion.
You can not alert() objects. So Use console.log() instead of alert().
By The Way you can alert() individual values.
Working snippet:-
var obj = JSON.parse('[{ "name":"John", "age":30, "city":"New York"}]');
console.log(obj);
alert(obj[0].name); // you can alert values
alert(obj[0].age); // you can alert values
alert(obj[0].city); // you can alert values
var test = '[{ "name":"John", "age":30, "city":"New York"}]';
var obj = eval ("(" + test + ")");
console.log(obj);
//console.log(obj[0].name);
You have an array in json object you can get the json object
values by referencing the key name.
alert(obj[0].name);
alert the value of json object at 0 index and by providing the key name.
<script>
var obj = JSON.parse('[{ "name":"John", "age":30, "city":"New York"}]');
alert(obj);
alert(obj[0].name);
</script>