dojo读取json,如何处理多个对象的数据?

Sugeei 2014-04-16 03:08:41
关于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 中有多个对象的时候的数据?

...全文
207 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
doudoudewang 2014-04-17
  • 打赏
  • 举报
回复
define(["dojo/json"], function(JSON){ var parsed = JSON.parse(jsonString); }); 这句话你懂的。
嘻哈大咖秀 2014-04-16
  • 打赏
  • 举报
回复
for(var index in json) 用循环处理

87,919

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧