使用JavaScript或jQuery读取数组JSON
我有下一个数组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":
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);