JQuery:从 JSON 读取值
JQuery: read value from json
我有以下代码:
var Configuration = {
conf : {},
buildConfig : function (){
var configugration = {};
configugration.supportedCountsInJSON = ["closedSRCount","resSRCount","openTAGCount","SECount"];
return configugration;
},
还有一个像这样的变量:
var teamCountsJson =
[ {
"associateId" : "AA029693",
"closedSRCount" : "10",
"resSRCount" : "8",
"openTAGCount" : "0",
"SECount" : "7",
},
{
"associateId" : "BB029693",
"closedSRCount" : "4",
"resSRCount" : "1",
"openTAGCount" : "0",
"SECount" : "1",
}]
当我尝试根据配置变量中的键作为 follws 从上述 Json 中读取值时,我得到"未定义":
calculateMinCounts: function(teamCountsJson){
$.each(teamCountsJson, function(index){
var minValues = [];
var sKey ='';
$.each(Configuration.conf.supportedCountsInJSON, function(id){
minValues = [];
sKey = Configuration.conf.supportedCountsInJSON[id]
if(teamCountsJson[index].hasOwnProperty(sKey)){
minValues.push(teamCountsJson[index].sKey);
console.log('sKey='+sKey+', minValues='+JSON.stringify(teamCountsJson[index].sKey)); //Unable to read the value 'undefined'
}
else{
//console.log('associateId does not exit!!'+Configuration.conf.supportedCountsInJSON[id]);
}
});
我知道
sKey = Configuration.conf.supportedCountsInJSON[id]
当我注销它们时,变量给了我以下键:
["closedSRCount","resSRCount","openTAGCount","SECount"];
但是,当我尝试在上面的代码中读取 Json(例如,associateId)中每个键的值时,如下所示,我得到"未定义"并且无法读取这些值:
teamCountsJson[index].sKey
有什么想法吗?
请指教,
谢谢!
没有sKey
属性,如果要使用包含属性名称的变量,则必须使用括号表示法
teamCountsJson[index][sKey]
若要访问 javscript 对象上的可变名称属性,需要使用[]
语法。
在代码中,定义所需的值 sKey
,然后尝试像这样访问它:
teamCountsJson[index].sKey
这是抓取teamCountsJson[index]
对象的 sKey 属性,该属性将undefined
.
您可能打算这样做:
teamCountsJson[index][sKey]
相关文章:
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 正在读取JSON,数据未显示
- 使用jquery读取Json数据
- 如何使用jquery ajax和jsonp在您自己的域上读取json数据
- 使用Angular读取JSON
- 修改d3.js圆环图以读取json数组
- 读取 json 文件 Javascriot 后返回一个数组
- 使用Javascript和PHP读取JSon字符串
- 无法通过rest api读取json变量
- 从d3.js中的文件读取json数据不起作用
- Uncaught SyntaxError:尝试读取json数据时出现意外数字
- 以javascript中的字符串形式从服务器读取json文件
- 使用angularjs中的JSONObject读取json
- 单击按钮时使用JS读取JSON文件
- 如果没有JavaScript中的jQuery,我如何读取.json文件
- javascript在读取JSON文件后无法迭代数组
- 使用 Javascript 读取 JSON 对象(错误)
- JQuery 如何读取 JSON 缓存
- 从 PHP 读取 json 响应并推送到 javascript 多维数组中
- 当我不知道密钥的名称时,如何读取 JSON 对象