我正在构建 Dojo 移动应用程序。我有一个像这样的 Json 文件:
{
Introduction:
[
{
title: "Introduction",
toolbar: "Page 1",
content: "cont, aabitant morbi tristique..."
},
{
title: "Introduction",
toolbar: "Page 2",
content: "contesent vel nisi ipsum..."
}
],
Services:
[
{
title: "services",
toolbar: "Page 1",
content: "Cras adipiscing sapien nec..."
}
]
}
以下代码打印标题中所写的介绍
dojo.xhrPost({
url: "diet.json",
handleAs: "json",
load: function(response) {
console.log(response.Introduction[0].title);
}
});
我能够获取内部数据。我怎样才能得到第一个标题,即
- 介绍
- 服务
最佳答案
所以您希望响应中每个对象的第一个标题?
for (key in response)
console.log(key + ": " + response[key][0].title);
当然,这假定响应中的每个数组中至少有一个元素。如果有些可能是空的,你会想要这样的东西:
for (key in response)
console.log(key + ": " +
(response[key].length > 0 ? response[key][0].title : "empty"));
关于javascript - 使用 dojo 显示 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8523327/