我正在尝试在这个JSON中选择avg,但我无法让它工作。我在这一刻的选择是data.user_stats.items[0]['avg']
,我正在尝试这样做:data.user_stats.items[0].['avg']
"data": {
"user": {
"id": "2",
"code": "user2",
},
"user_stats": {
"items": {
"ITEM-01-Alumno": {
"sum": 0,
"qty": 6,
"avg": 0,
"exercises": {
"ITEM-1": "0.00"
}
},
"ITEM-02-Alumno": {
"sum": 10,
"qty": 1,
"avg": 10,
"exercises": {
"ITEM-2": 10
}
}
答案 0 :(得分:1)
items
内部不是数组,而是一个对象。您必须通过以下方式选择它:
data.user_stats.items['ITEM-01-Alumno'].avg
当然,您可以将其更改为数组,但是您将丢失标签。除非您将名称移动到对象数据中:
"data": {
"user": {
"id": "2",
"code": "user2",
},
"user_stats": {
"items": [
{
"label": "ITEM-01-Alumno",
"sum": 0,
"qty": 6,
"avg": 0,
"exercises": {
"ITEM-1": "0.00"
}
},
{
"label": "ITEM-02-Alumno",
"sum": 10,
"qty": 1,
"avg": 10,
"exercises": {
"ITEM-2": 10
}
}
]
}
}
您可能还想查看exercises
部分,除非您希望稍后执行for...in
循环或Object.keys
,否则它也可能会从重组中受益。