我想从下面给定的 json 中提取tenantId值
{
"ClientAccounts":{
"@tenantId":"entpriseDemo",
"clientAccount":[
{
"guid":"447a0bac-51e0-4f5f-b504-97dca5825530",
"totalValueFormatted":"$1,100,000"
}
]
}
}
这是我的javascript函数,用于调用ajax请求和成功函数代码
$.ajax({
url: "$clientAccountsURL",
cache: false,
dataType: "json", // set to json or xml
success: function(data){
alert(data.ClientAccounts.tenantId);
}
});
当我在 success 函数中提醒tenantId 的值时,它返回了未定义的值,尽管我检查了 firebug 它在 json 数组中是否可用。
是否有其他方法来检索tenantId的值。
最佳答案
使用方括号表示法
data.ClientAccounts['@tenantId']
来自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.
关于javascript - 从 JSON 中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18610868/