JavaScript/JSON:获取对象的未知属性 [英] JavaScript/JSON: Get unknown property of an object
本文介绍了JavaScript/JSON:获取对象的未知属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的JSON对象:
If I have a JSON object like this:
{
"message": {
"name": { "stringLengthTooShort": "blub" }
}
}
属性名称(此处为stringLengthTooShort
)每次都在变化,
我怎么能只用JS获得name
的child属性?
目前我有message.name
,但是现在我怎么能得到它的孩子?
The name of the property (here) stringLengthTooShort
is changing every time,
how could I simply just get the child property of name
with JS?
At the moment I have message.name
but how could I get now the child of it?
推荐答案
如果它始终是message.name
的第一个属性,则可以执行以下操作:
if it's always the first property of message.name
, you could do something like:
var keys = [];
for (var l in message.name) {
if (message.name.hasOwnProperty(l)){
keys.push(l);
}
}
//=>first property value should now be in message.name[keys[0]]);
// (its label is keys[0])
这篇关于JavaScript/JSON:获取对象的未知属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文