使用嵌入式数组迭代JSON
Iterating JSON with embedded array
我有以下JSON:
[
{
"id": "1",
"selected": true,
"question": "Which of the following does <b><i>not</i></b> describe Washington’s location?",
"answerswers": {
"A": {
"selector": "A",
"answerswerText": "It is in the northwest corner of the United States.",
"correct": "N"
},
"B": {
"selector": "B",
"answerswerText": "The Pacific Ocean provides the western border.",
"correct": "N"
},
"C": {
"selector": "C",
"answerswerText": "It is north of Oregon and west of Idaho.</span>",
"correct": "N"
},
"D": {
"selector": "D",
"answerswerText": "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?",
"answerswers": {
"A": {
"selector": "A",
"answerswerText": "Most people settled along rivers and water in the fertile valleys.",
"correct": "Y"
},
"B": {
"selector": "B",
"answerswerText": "Most people settled high in the mountains to protect themselves from their enemies.",
"correct": "N"
},
"C": {
"selector": "C",
"answerswerText": "Most people settled at the base of the Rocky Mountains. They couldn’t travel any further.",
"correct": "N"
},
"D": {
"selector": "D",
"answerswerText": "Most people settled along the Pacific Rim because it was a good place to trade.",
"correct": "N"
}
}
}
]
迭代对象的最佳方式是什么?我应该使用jquery还是直接使用javascript?任何一个例子都很好。。。
要将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]
}
}
我特别喜欢这里和这里描述的$.parseJSON
。
相关文章:
- 如何在Jquery中迭代JSON数组
- 如何迭代json对象结构以只获取名称
- 迭代 JSON 文件并且未在 Javascript 中正确返回结果
- 如何迭代json对象的嵌套属性并创建新的数组列表
- 迭代JSON对象并将元素打印到屏幕上的最佳方式是什么
- 无法使用jquery迭代json对象
- 使用嵌入式数组迭代JSON
- 如何使用 D3 javascript 迭代 JSON 文件中的数据
- 如何解析 JSON 字符串和迭代 JSON 对象
- 如何使用Jade和Node.js迭代JSON数组
- 访问特定属性并在 AngularJS 中迭代 JSON 数组
- 如何在 jsRender 模板中迭代 JSON 数组
- 如何使用jQuery迭代json数组和拉取值
- Javascript - 递归 - 迭代 json,用户能够跳过
- 翡翠混合迭代 json
- 从 stomp 消息响应 (JavaScript) 迭代 json 数组
- 在 coffeescript 中迭代 json 响应文本
- 迭代 json 响应
- Javascript 按索引迭代 json
- 在 Angular 项目中迭代 JSON 响应以创建新的 $scope obj 我可以在应用程序中使用的