在 JavaScript 中获取 json-object 的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8430336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get keys of json-object in JavaScript
提问by user1027167
I have a json-object in JavaScript and I want to get the used keys in it. My JavaScript-Code looks like this:
我在 JavaScript 中有一个 json 对象,我想在其中获取使用过的键。我的 JavaScript 代码如下所示:
var jsonData = [{"person":"me","age":"30"},{"person":"you","age":"25"}];
And I want a loop that alerts me 'person' and 'age', which are the keys of the first object in the json-Array.
我想要一个循环来提醒我“人”和“年龄”,它们是 json-Array 中第一个对象的键。
采纳答案by T.J. Crowder
[What you have is just an object, not a "json-object". JSONis a textual notation. What you've quoted is JavaScript code using an array initializerand an object initializer(aka, "object literal syntax").]
[你所拥有的只是一个对象,而不是“json-object”。JSON是一种文本符号。您引用的是使用数组初始值设定项和对象初始值设定项(又名“对象文字语法”)的JavaScript 代码。]
If you can rely on having ECMAScript5 features available, you can use the Object.keys
function to get an array of the keys (property names) in an object. All modern browsers have Object.keys
(including IE9+).
如果您可以依靠 ECMAScript5 功能可用,您可以使用该Object.keys
函数来获取对象中的键(属性名称)数组。所有现代浏览器都有Object.keys
(包括 IE9+)。
Object.keys(jsonData).forEach(function(key) {
var value = jsonData[key];
// ...
});
The rest of this answer was written in 2011. In today's world, A) You don't need to polyfill this unless you need to support IE8 or earlier (!), and B) If you did, you wouldn't do it with a one-off you wrote yourself or grabbed from an SO answer (and probably shouldn't have in 2011, either). You'd use a curated polyfill, possibly from es5-shim
or via a transpiler like Babel that can be configured to include polyfills (which may come from es5-shim
).
这个答案的其余部分写于 2011 年。在当今世界,A)除非您需要支持 IE8 或更早版本(!),否则您不需要对其进行 polyfill,并且 B)如果您这样做了,您就不会这样做您自己写的或从 SO 答案中获取的一次性(可能也不应该在 2011 年有)。您将使用es5-shim
精选的 polyfill ,可能来自或通过诸如 Babel 之类的转译器,可以配置为包含 polyfill(可能来自es5-shim
)。
Here's the rest of the answer from 2011:
以下是 2011 年的其余答案:
Note that older browsers won't have it. If not, this is one of the ones you can supply yourself:
请注意,较旧的浏览器不会有它。如果没有,这是您可以自己提供的其中之一:
if (typeof Object.keys !== "function") {
(function() {
var hasOwn = Object.prototype.hasOwnProperty;
Object.keys = Object_keys;
function Object_keys(obj) {
var keys = [], name;
for (name in obj) {
if (hasOwn.call(obj, name)) {
keys.push(name);
}
}
return keys;
}
})();
}
That uses a for..in
loop(more info here) to loop through all of the property names the object has, and uses Object.prototype.hasOwnProperty
to check that the property is owned directly by the object rather than being inherited.
它使用一个for..in
循环(此处有更多信息)来遍历对象具有的所有属性名称,并用于Object.prototype.hasOwnProperty
检查该属性是否由对象直接拥有而不是被继承。
(I could have done it without the self-executing function, but I prefer my functions to have names, and to be compatible with IE you can't use named function expressions[well, not without great care]. So the self-executing function is there to avoid having the function declaration create a global symbol.)
(我可以在没有自执行函数的情况下完成它,但我更喜欢我的函数有名称,并且为了与 IE 兼容,你不能使用命名函数表达式[好吧,不是没有非常小心]。所以自执行函数是为了避免函数声明创建全局符号。)
回答by Ranganadh Paramkusam
var jsonData = [{"person":"me","age":"30"},{"person":"you","age":"25"}];
for(var i in jsonData){
var key = i;
var val = jsonData[i];
for(var j in val){
var sub_key = j;
var sub_val = val[j];
console.log(sub_key);
}
}
EDIT
编辑
var jsonObj = {"person":"me","age":"30"};
Object.keys(jsonObj); // returns ["person", "age"]
Object
has a property
keys
, returns an Array
of keys from that Object
Object
有一个property
keys
,Array
从中返回一个键Object
Chrome, FF & Safari supports Object.keys
支持 Chrome、FF 和 Safari Object.keys
回答by Sandeep G B
The working code
工作代码
var jsonData = [{person:"me", age :"30"},{person:"you",age:"25"}];
for(var obj in jsonData){
if(jsonData.hasOwnProperty(obj)){
for(var prop in jsonData[obj]){
if(jsonData[obj].hasOwnProperty(prop)){
alert(prop + ':' + jsonData[obj][prop]);
}
}
}
}