dojo读取json,如何处理多个对象的数据?
关于dojo 解析json, 获取json文件中的某个key的value.
问题背景描述如下:
Test.html
<!DOCTYPE html>
<html >
<head>
<linkrel="stylesheet" href="dijit/themes/claro/claro.css">
<styletype="text/css">
@import "dojox/grid/resources/claroGrid.css";
/*Grid needs an explicit height bydefault*/
#gridDiv {
height: 20em;
}
</style>
<script>dojoConfig= {async: true, parseOnLoad: false}</script>
<scriptsrc='dojo/dojo.js'></script>
<scripttype="text/javascript" src="dojo_ajax_read_json.js"></script>
</head>
<body>
<h1>Output:</h1>
<div id="output"></div>
<button type="button" id="startButton">Start</button>
</body>
</html>
在html 文件中引用了dojo_ajax_read_json.js 文件,如下:
dojo_ajax_read_json.js
require(["dojo/request/xhr","dojo/dom", "dojo/dom-construct", "dojo/json", "dojo/on","dojo/domReady!"],
function(xhr, dom, domConst, JSON, on){
domConst.place("<p>Requesting...</p>", "output");
xhr("hello.json",{
query: {
key1: "value1",
key2: "value2"
},
handleAs: "json"
}).then(function(data){
domConst.place("<p>response: <code>" + JSON.stringify(data)+ "</code></p>", "output");
});
});
主要通过 domConst.place("<p>response: <code>" + JSON.stringify(data)+ "</code></p>", "output");
这一句对从文件中读取json对象并将它显示在页面中。
hello.json 文件内容如下:
{
"items":[
{
"id":"1",
"question":"______came that London would hold the Olympic Gamesin 2012 .",
"choice":[{"fir":"word"},{"sec":"the word"},{"thr":"words"},{"fou":""}],
"answer":"1",
"analysis":"Word came that"
}
]
}
可以看到,此json文件中有一个items 的对象。
页面显示正常,效果如下,可以正常返回读取到的json.
Output:
Requesting...
response: {"items":[{"id":"1","question":"______camethat London would hold the Olympic Games in 2012.","choice":[{"fir":"word"},{"sec":"theword"},{"thr":"words"},{"fou":""}],"answer":"1","analysis":"Wordcame that"}]}
问题描述:
问题是当items 中不止一个对象的时候,文件如下,修改后的hello.json:
{
"items":[
{
"id":"1",
"question":"______came that London would hold the Olympic Gamesin 2012 .",
"choice":[{"fir":"word"},{"sec":"the word"},{"thr":"words"},{"fou":""}],
"answer":"1",
"analysis":"Word came that"
}
,{
"id":"2",
"question":" Themanager discussed the plan that they would like to see____the next year. ",
"choice":[{"fir":"carry out"},{"sec":"carrying out"},{"thr":"carried out"},{"fou":"to carry out"}],
"answer":"3",
"analysis":""
}
,{
"id":"3",
"question":"You're drinking too much.---Only at home.Noone___me but you",
"choice":[{"fir":"is seeing"},{"sec":"had seen"},{"thr":"sees"},{"fou":"saw"}],
"answer":"3",
"analysis":""
}
]
}
结果是页面无显示,无法正常返回读取到的json对象。
请问dojo如何处理对象数组中的数据,即json 中有多个对象的时候的数据?