Javascript 使用嵌入式数组迭代JSON
我有以下JSON:Javascript 使用嵌入式数组迭代JSON,javascript,jquery,json,Javascript,Jquery,Json,我有以下JSON: [ { "id": "1", "selected": true, "question": "Which of the following does <b><i>not</i></b> describe Washington’s location?", "answers": { "A": { "selec
[
{
"id": "1",
"selected": true,
"question": "Which of the following does <b><i>not</i></b> describe Washington’s location?",
"answers": {
"A": {
"selector": "A",
"answerText": "It is in the northwest corner of the United States.",
"correct": "N"
},
"B": {
"selector": "B",
"answerText": "The Pacific Ocean provides the western border.",
"correct": "N"
},
"C": {
"selector": "C",
"answerText": "It is north of Oregon and west of Idaho.</span>",
"correct": "N"
},
"D": {
"selector": "D",
"answerText": "A natural boundary can be created by a river.",
"correct": "Y"
}
}
},
{
"id": "2",
"selected": true,
"question": "Which of the following best describes a spatial pattern in Washington?",
"answers": {
"A": {
"selector": "A",
"answerText": "Most people settled along rivers and water in the fertile valleys.",
"correct": "Y"
},
"B": {
"selector": "B",
"answerText": "Most people settled high in the mountains to protect themselves from their enemies.",
"correct": "N"
},
"C": {
"selector": "C",
"answerText": "Most people settled at the base of the Rocky Mountains. They couldn’t travel any further.",
"correct": "N"
},
"D": {
"selector": "D",
"answerText": "Most people settled along the Pacific Rim because it was a good place to trade.",
"correct": "N"
}
}
}
]
[
{
“id”:“1”,
“选定”:正确,
“问题”:“以下哪项不描述华盛顿的位置?”,
“答案”:{
“A”:{
“选择器”:“A”,
“答案文本”:“它位于美国西北角。”,
“正确”:“N”
},
“B”:{
“选择器”:“B”,
“答案文本”:“太平洋提供了西部边界。”,
“正确”:“N”
},
“C”:{
“选择器”:“C”,
“答案文本”:“它位于俄勒冈州北部,爱达荷州西部。”,
“正确”:“N”
},
“D”:{
“选择器”:“D”,
“answerText”:“河流可以创造自然边界。”,
“正确”:“Y”
}
}
},
{
“id”:“2”,
“选定”:正确,
“问题”:“以下哪项最能描述华盛顿的空间格局?”,
“答案”:{
“A”:{
“选择器”:“A”,
“答案文本”:“大多数人定居在肥沃的山谷中的河流和水域。”,
“正确”:“Y”
},
“B”:{
“选择器”:“B”,
“答案文字”:“大多数人定居在高山上是为了保护自己免受敌人的伤害。”,
“正确”:“N”
},
“C”:{
“选择器”:“C”,
“答案文字”:“大多数人定居在落基山脉的底部。他们不能再往前走了。”,
“正确”:“N”
},
“D”:{
“选择器”:“D”,
“答案文本”:“大多数人在环太平洋地区定居,因为那里是一个贸易的好地方。”,
“正确”:“N”
}
}
}
]
迭代对象的最佳方式是什么?我应该使用jquery还是纯javascript?任何示例都会很好…我特别喜欢
$.parseJSON
,如前所述和。将JSON字符串解析为对象使用
JSON.parse(str);
要遍历数组,请使用
for(var i=0, l=arr.length; i<l; ++i) {
// Here use arr[i]
}
for(var i in obj) {
if(obj.hasOwnProperty(i)) {
// Here use obj[i]
}
}