您如何在JS中的JSON中访问阵列中的对象



我正在尝试循环通过我从php通过ajax传递给JS脚本的一些数据。

数据是:

history: [
["1h", {
    "number": "8651",
    "event": "lock"
}],
["2h", {
    "number": "16456",
    "event": "edit"
}],
["2h", {
    "number": "90",
    "event": "edit"
}],
]

我使用jQuery用:

解析了JSON
var responseData = $.parseJSON(data);
history = JSON.parse(responseData.history);

但是,我尝试访问此数据的每一次尝试都会引发错误,或返回有关对象的元信息。

$.each(history, function (index, value) {
    console.log(value);
});

返回:

function go()
function back()
function forward()
function pushState()
function replaceState()
11
auto
null

如何访问数据?

解析两次的原因是

console.log(typeof responseData.history); // 'string'

尝试:

$.each(responseData.history, function (index, value) {

根据gurvinder372的答案给出了错误:

TypeError: cannot use 'in' operator to search for '(b - 1)' in '[["1h",{"number"...'

您无法通过分配编辑window.history,因此您要迭代window.history对象,迭代responseData.history

$.each(responseData.history, function (index, value) {

另外,根据您的代码

var responseData = $.parseJSON(data);

responseData被解析为对象。

最新更新