How to access JSON Object name/value? How to access JSON Object name/value? ajax ajax

How to access JSON Object name/value?


In stead of parsing JSON you can do like followng:

$.ajax({  ..  dataType: 'json' // using json, jquery will make parse for  you});

To access a property of your JSON do following:

data[0].name;data[0].address;

Why you need data[0] because data is an array, so to its content retrieve you need data[0] (first element), which gives you an object {"name":"myName" ,"address": "myAddress" }.

And to access property of an object rule is:

Object.property

or sometimes

Object["property"] // in some case

So you need

data[0].name and so on to get what you want.


If you not

set dataType: json then you need to parse them using $.parseJSON() and to retrieve data like above.


The JSON you are receiving is in string. You have to convert it into JSON objectYou have commented the most important line of code

data = JSON.parse(data);

Or if you are using jQuery

data = $.parseJSON(data)


If you response is like {'customer':{'first_name':'John','last_name':'Cena'}}

var d = JSON.parse(response);alert(d.customer.first_name); // contains "John"

Thanks,