javascript - 从 JSON 中检索值

标签 javascript ajax json

我想从下面给定的 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/

相关文章:

javascript - 如何最好地确定元素/节点在 DOM 中的距离?

javascript - 请求动画帧返回结果?

ajax - Polymer.js + OAuth2

ajax - jQuery 和 Ajax with json - 在 IE 中失败

php - 实时更新 MySQL 数据

javascript - 有没有简单的方法来回转换这个 JSON?

javascript - 单击按钮后 JqGrid 不会重新加载

javascript - 谷歌地图 - 在标记点击中心 map

javascript - 如何使用 AngularJS 的 ngRepeat 指令从嵌套的 JSON 对象动态生成 HTML 表?

iphone - 为 iPhone 创建 JSON 存储