Javascript 获取JSON数组中的键值
在循环遍历JSON数组时,我试图获取每个记录的键值。目前我有一个简单的JSON对象,如下所示:Javascript 获取JSON数组中的键值,javascript,jquery,json,Javascript,Jquery,Json,在循环遍历JSON数组时,我试图获取每个记录的键值。目前我有一个简单的JSON对象,如下所示: "users": { "key_11": { "text": "11" }, "key_22": { "text": "22" }, "key_33": { "text": "33" } } 我当前的脚本使用“map”方法将此JSON对象转换为可循环数组: var user_profiles_array = $.
"users": {
"key_11": {
"text": "11"
},
"key_22": {
"text": "22"
},
"key_33": {
"text": "33"
}
}
我当前的脚本使用“map”方法将此JSON对象转换为可循环数组:
var user_profiles_array = $.map(user_profiles_string, function(el) { return el; });
for (var xt = 0; xt < user_profiles_array.length; xt++) {
console.log(user_profiles_array[xt].text); //11 or 22
}
var user\u profiles\u array=$.map(user\u profiles\u字符串,函数(el){return el;});
对于(var xt=0;xt
我的问题是,如何获取例如“key_11”或“key_22”的值
谢谢 可以使用获取对象所有关键帧的数组。获得该数组后,可以根据需要使用来迭代该数组:
Object.keys(usersObject).forEach(function(key, keyIndex) {
console.log("index:",keyIndex,"key:",key,"value:",usersObject[key]);
});
但是
这里的特定问题是使用$.map
而不是JSON.parse
造成的$.map
返回一个数组,因此您的键当然总是数字数组索引-0
,1
,2
,等等。您将无法使用哈希键在$.map
返回的数组中查找内容。此外,根据变量名判断,您正在调用字符串上的$.map
,而该字符串肯定不会满足您的要求。假设您计算出了该部分,并且以某种方式获得了一个有效的JavaScript对象,并且出于某种原因仍然需要使用$.map()
,那么您可以做的是:
// $.map will return an array...
$.map(user_profiles_object, function(objVal, objKey) {
// ...and each item in that array will be an object with a
// property named 'key' and a property named 'val'
return {
key: objKey,
val: objVal
};
}).forEach(function(arrayObj) {
// now each item in the array created above will be an object
// created by your callback function:
console.log(arrayObj.key,":",arrayObj.val);
});
可以使用获取对象所有键的数组。获得该数组后,可以根据需要使用来迭代该数组:
Object.keys(usersObject).forEach(function(key, keyIndex) {
console.log("index:",keyIndex,"key:",key,"value:",usersObject[key]);
});
但是
这里的特定问题是使用$.map
而不是JSON.parse
造成的$.map
返回一个数组,因此您的键当然总是数字数组索引-0
,1
,2
,等等。您将无法使用哈希键在$.map
返回的数组中查找内容。此外,根据变量名判断,您正在调用字符串上的$.map
,而该字符串肯定不会满足您的要求。假设您计算出了该部分,并且以某种方式获得了一个有效的JavaScript对象,并且出于某种原因仍然需要使用$.map()
,那么您可以做的是:
// $.map will return an array...
$.map(user_profiles_object, function(objVal, objKey) {
// ...and each item in that array will be an object with a
// property named 'key' and a property named 'val'
return {
key: objKey,
val: objVal
};
}).forEach(function(arrayObj) {
// now each item in the array created above will be an object
// created by your callback function:
console.log(arrayObj.key,":",arrayObj.val);
});
您还可以依赖Js的foreach
// JSON string must be valid. Enclose your JSON in '{}' (curly braces);
var user_profiles_string = '{ "users": { "key_11": { "text": "11" }, "key_22": { "text": "22" }, "key_33": { "text": "33" }}}';
var user_profiles_array = JSON.parse(user_profiles_string);
// For retrieval in loop, the Js foreach asigns the key to index param (i in this case).
for (i in user_profiles_array.users) {
// i is the key of the user currently iterated.
console.log('Key name is: ' + i);
// Use i as the index to retrieve array value.
console.log(user_profiles_array.users[i]);
}
// For direct retrieval using any given known key:
console.log(user_profiles_array.users['key_11']);
您还可以依赖Js的foreach
// JSON string must be valid. Enclose your JSON in '{}' (curly braces);
var user_profiles_string = '{ "users": { "key_11": { "text": "11" }, "key_22": { "text": "22" }, "key_33": { "text": "33" }}}';
var user_profiles_array = JSON.parse(user_profiles_string);
// For retrieval in loop, the Js foreach asigns the key to index param (i in this case).
for (i in user_profiles_array.users) {
// i is the key of the user currently iterated.
console.log('Key name is: ' + i);
// Use i as the index to retrieve array value.
console.log(user_profiles_array.users[i]);
}
// For direct retrieval using any given known key:
console.log(user_profiles_array.users['key_11']);
users.key_11.text
是“key_11”对象的“text”属性的值。是的,但我需要一种方法来获取“key_11”和“key_22”以及“key_33”等,而不是文本属性的值。它返回给定对象的属性名称数组。使用此函数我得到[0,1,2,3,4,5],但不是[key_11,key_22,key_33]object.keys(您的\u OBJECT\u VARIABLE.users)
.Look:users.key\u 11.text
是“key\u 11”对象的“text”属性的值。是的,但我需要一种方法来获取“key\u 11”、“key\u 22”和“key\u 33”等,而不是文本属性的值返回给定对象的属性名称数组。使用此函数我得到[0,1,2,3,4,5],但不是[key\u 11,key\u 22,key\u 33]Object.keys(你的对象变量.users)
。看:'usersObject'是保存我转换数组的变量吗?是的,但是你使用$.map
有什么原因吗?因为我想这就是为什么你把“0”、“1”等看作数组索引而不是“key\u 11”、“key\u 22”等。$.map
正在将对象转换为数组-JSON.parse
只是解析字符串JSON?我使用$.map的原因是JSON.parse对我不起作用。我不断收到以下错误:“JSON.parse:JSON数据第1行第2列的意外字符”我正在尝试解析Firebase JSON文件,可能是错误的因为他们的文件格式有点不同?我敢打赌,你的JSON格式不正确。请尝试用大括号将字符串括起来,然后尝试解析:console.log(JSON.parse(“{+user\u profiles\u string+”))“
usersObject”是保存我转换数组的变量吗?是的,但是你使用$.map
而不使用它有什么原因吗?因为我认为这就是为什么你将“0”、“1”等视为数组索引,而不是“key\u 11”、“key\u 22”等。$.map
正在将对象转换为数组-JSON.parse
只是解析字符串JSON?我使用$.map的原因是JSON.parse对我不起作用。我不断收到以下错误:“JSON.parse:JSON数据第1行第2列的意外字符”我正在尝试解析Firebase JSON文件,可能是错误的因为他们的文件格式有点不同?我敢打赌,您的JSON格式不正确。请尝试将字符串用大括号括起来,然后尝试对其进行解析:console.log(JSON.parse(“{+user\u profiles\u string+”}”);