Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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或jQuery读取数组JSON_Javascript_Arrays_Json - Fatal编程技术网

使用JavaScript或jQuery读取数组JSON

使用JavaScript或jQuery读取数组JSON,javascript,arrays,json,Javascript,Arrays,Json,我有下一个数组json,我如何读取它? 我已经尝试过使用document.write(arr),但如果我将document.write(arr[0].Category)放入,则会显示未定义的 var arr = [{"Category": {"cell":{"question1":{"que1":"What is the cell?"}, "option1":{"op1":"The cell is the basic structural and functional unit", "op2":

我有下一个数组json,我如何读取它? 我已经尝试过使用
document.write(arr)
,但如果我将
document.write(arr[0].Category)
放入,则会显示
未定义的

var arr = [{"Category":
{"cell":{"question1":{"que1":"What is the cell?"},
"option1":{"op1":"The cell is the basic structural and functional unit",
"op2":"is a fictional supervillain in Dragon Ball"},"answer1":"opt1"}}},
{"Category":{"Mars":{"question1":{"que1":"How many moons does Mars?"},
"option1":{"op1":"5","op2":"2"},"answer1":"opt2"}}}]

顺便说一句,数组的格式很好,因为如果我执行
document.write(arr)
操作,它将返回相同的数组代码确实有效。看:

记录对象,
写入:
单元格是什么?

document.write(arr[0].Category) 
写入
[object object]
,未定义

问题不在给出的代码中


编辑:更多信息。OP在评论中添加了“我需要说,数组是从json文件中提取出来的,我做了一个var arr=json.stringify(arrjson)”

如果你把arr设为一个字符串,它就不起作用了。你举的例子!=你的问题

document.write(arrjson[0].Category);

是您要查找的对象。

它应该可以正常工作
document.write()
不是检查变量的最佳方法。改用
console.log()
。这不是一个JSON字符串,而是一个简单的对象/数组文本!我需要说,数组是从json文件中提取的,我做了一个var arr=json.stringify(arrjson),然后使用
arrjson[0].Category
arr
是一个字符串。arr不是给定代码中的字符串,@chikatetsu您在哪里执行的
var arr=JSON.stringify(arrjson)
?这与您提供的代码冲突。
document.write(arrjson[0].Category);