【发布时间】:2014-07-17 07:34:37
【问题描述】:
我创建了一个 JSON 对象,现在当我尝试从 JSON 对象打印数据时,我在控制台上的 document.write 和 undefined 消息上没有得到任何输出。
请检查我的代码并指导我在哪里犯了错误。
<head>
<script>
var variable=
[
{
"first_name": "Steve",
"last_name": "Jobs",
"Education" : [
{"Intermediate" : "2006" },
{"Bachelors" : "2009" },
{"Masters" : "2015" }
]
},
{
"first_name": "Bill",
"last_name": "Gates",
"Education": [
{"Intermediate" : "2010"},
{"Bachelors" : "2012"},
{"Masters" : "2014"}
]
}
];
console.log(variable.first_name);
for (key in variable.Education)
{
console.log(key);
}
</script>
</head>
【问题讨论】:
-
console.log(variable[0].first_name); -
@SridharR 是正确的。你在这里拥有的不仅仅是一个对象,它是一个包含一个对象的数组。所以你必须访问数组才能得到对象。
-
感谢它的工作..但我怎么能在 for 循环中做到这一点?它在
Console.log和document.write上给了我输出...但是在循环中它没有显示任何东西。
标签: javascript jquery html xml json