JavaScript/JSON: Get unknown property of an object

2020-02-01 04:44发布

If I have a JSON object like this:

{
    "message": {
        "name": { "stringLengthTooShort": "blub" }
    }
}

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?

1条回答
虎瘦雄心在
2楼-- · 2020-02-01 05:02

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])
查看更多
登录 后发表回答