Vit*_*kov 1 javascript json object
我有一个JSON对象返回,格式如下:
"miscObject": {
"205": [
{
"h": "Foo",
"l": "Bar"
}
]
}
Run Code Online (Sandbox Code Playgroud)
miscObject包含超过1500个条目,每个条目以递增方式命名.
如果我将"205"存储在变量中而不必遍历miscObject中的所有对象,我怎样才能获得'miscObject.205.h'和'miscObject.205.l'的值?
您似乎在谈论Javascript对象而不是JSON字符串.
x[y]并且x.y在访问Javascript对象的属性时大多可以互换,区别在于y前者可能是表达式.
利用这个来解决您的问题,如下所示:
var num = '205';
console.log(miscObject[num].l);
// ^^^^^
// \
// instead of `.num`, which wouldn't be the same as
// `num` is not the literal name of the property
Run Code Online (Sandbox Code Playgroud)