我是 JSON 和 ajax 的新手,但我正在尝试访问数组中的数据,其中的项目在另一个子数组中的子数组中枚举。我在尝试访问类似内容时遇到了问题
data.items[0].details.specs[1].name
data.items[0].details.specs[1].id
data.items[0].details.specs[2].name
data.items[0].details.specs[2].id
etc
任何人都可以指出正确的方向以正确访问它吗?谢谢。
更新:这是对我的问题的澄清,JSON 响应是什么样子的:
{data:{
items: [
{details: {specs:[{name:'something1', id:'something1'},
{name:'something2', id:'something2'},
{name:'something3', id:'something3'},
...
]}}
{details: {specs:[{name:'somethingA', id:'somethingA'},
{name:'somethingB', id:'somethingB'},
{name:'somethingC', id:'somethingC'},
...
]}}
{details: {specs:[{name:'somethingX', id:'somethingX'},
{name:'somethingY', id:'somethingY'},
{name:'somethingZ', id:'somethingZ'},
...
]}}
]}
}
例如,我将如何访问“name:somethingB”?
最佳答案
可能只是 specs 不是对象数组。
你的 json 需要像这样才能访问那种方式
{
data:{
items: [
{details:
{specs:[
{name:'something1', id:'something1'},
{name:'something2', id:'something2'},
{name:'something3', id:'something3'},
...
]
}
}
]
}
}
编辑:根据OP,这是浏览器缓存问题
关于javascript - 在没有 getJSON 的情况下从 Javascript 中的枚举 JSON 子数组中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2696218/