How to access JSON object in JavaScript?

Answered

How to access JSON object in JavaScript?

Ninja Asked on 19th September 2018 in JSON.
Add Comment
1 Answer(s)
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>

Ninja Answered on 19th September 2018.
Add Comment