How to access JSON object in JavaScript?
Answered
How to access JSON object in JavaScript?
Best answer
JSON object can be accessed in JavaScript using JSON.parse() method and the values can be accessed using “.” or “[]”. The JSON.parse() method parses a string as JSON.
<script>
var text = ‘{“friends”:[‘ +
‘{“firstName”:”Nitin”,”lastName”:”K L” },’ +
‘{“firstName”:”Nikhil”,”lastName”:”Dhankani” },’ +
‘{“firstName”:”Ananya”,”lastName”:”S A” }]}’;
obj = JSON.parse(text);
alert(obj.friends[0].firstName+” “+obj.friends[0].lastName); // Displays Nitin K L
</script>