Reputation: 541
I want to extract the tenantId value from the below given json
{
"ClientAccounts":{
"@tenantId":"entpriseDemo",
"clientAccount":[
{
"guid":"447a0bac-51e0-4f5f-b504-97dca5825530",
"totalValueFormatted":"$1,100,000"
}
]
}
}
This is my javascript function for calling an ajax request and the success function code
$.ajax({
url: "$clientAccountsURL",
cache: false,
dataType: "json", // set to json or xml
success: function(data){
alert(data.ClientAccounts.tenantId);
}
});
When I'm alerting the value of tenantId in the success function it's returning me undefined value though i checked on the firebug its available in the json array.
Is there any another way to retrieve the value of tenantId.
Upvotes: 0
Views: 114
Reputation: 470
You can use either object notation or and associative array notation:
In your case, use associative array notation:
alert(data.ClientAccounts["@tenantId"]);
example: http://jsfiddle.net/2kdWQ/1/
Upvotes: 1
Reputation: 94499
Since the property name contains an @
you will have to access the property using the associative array syntax.
data.ClientAccounts["@tenantId"];
JsFiddle: http://jsfiddle.net/VS9xe/
Upvotes: 1
Reputation: 1007
You should be able to access it by using it like an associative array:
data.ClientAccounts['@tenantId']
Upvotes: 1
Reputation: 31590
Use the square brackets notation
data.ClientAccounts['@tenantId']
From this mdn article:
An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation.
Upvotes: 2