我正在构建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);
}
});
我能够获得内部数据。我怎样才能得到第一个标题,即
答案 0 :(得分:3)
所以你想要回复中每个对象的第一个标题?
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"));