使用JavaScript或jQuery读取数组JSON

Reading array JSON with JavaScript or jQuery

本文关键字:数组 JSON 读取 jQuery JavaScript 使用      更新时间:2023-09-26

我有下一个数组json,我如何读取它?我已经尝试过document.write(arr),它有效,但如果我放document.write(arr[0].Category),它会显示undefined

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"},"answerswer1":"opt1"}}},
{"Category":{"Mars":{"question1":{"que1":"How many moons does Mars?"},
"option1":{"op1":"5","op2":"2"},"answerswer1":"opt2"}}}]

顺便说一句,数组格式很好,因为如果我执行document.write(arr),它将返回相同的数组

代码确实有效。看:http://jsfiddle.net/5GTWp/

console.log(arr[0].Category);
document.write(arr[0].Category.cell.question1.que1);

记录对象,
写入:What is the cell?

document.write(arr[0].Category) 

写入[object Object],未定义。

问题不在给定的代码中。

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

如果你把arr做成一根绳子,它是行不通的。你举的例子!=你的问题。

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

是您要查找的对象。