Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从JSON中的JSON访问数组中的对象?_Javascript_Jquery_Json - Fatal编程技术网

Javascript 如何从JSON中的JSON访问数组中的对象?

Javascript 如何从JSON中的JSON访问数组中的对象?,javascript,jquery,json,Javascript,Jquery,Json,我试图循环使用一些数据,这些数据是我通过AJAX从PHP传递到JS脚本的 数据如下: history: [ ["1h", { "number": "8651", "event": "lock" }], ["2h", { "number": "16456", "event": "edit" }], ["2h", { "number": "90", "event": "edit" }], ] 我已经使用Jquery解析了JSON: var respo

我试图循环使用一些数据,这些数据是我通过AJAX从PHP传递到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
被解析为一个对象。

您解析了两次。您无法解析已经解析过的JSON。隐式全局赋值很好。@JaredSmith这是唯一可以解释OP在迭代
history
responseData
时得到的输出的东西,他可能已经是一个JSON了
responseData
已存在parsed@connexo这就是我的意思,尽管更新了。我尝试切换到
responseData.history
,但它似乎没有被解析,仍然是一个字符串。我已经用结果更新了问题。