【发布时间】:2012-01-16 12:15:24
【问题描述】:
{
"Adam":{
"Math":"93",
"Science":"96",
"Email":"adam@example.com",
"City":"NY"
},
"Tom":{
"Math":"65",
"Science":"56",
"Email":"tom@example.com",
"City":"LA"
},
"Joe":{
"Math":"74",
"Science":"83",
"Email":"joe@example.com",
"City":"Washington"
}
}
以上是 http://ActualUrl/path.json 处的 JSON 内容
我正在访问 JSON 文件并使用下面的代码在两个数组中填充名称和科学标记。
var names=[];
var marks=[];
$.getJSON("http://path.json",function(data){
$.each(data, function(key, val) {
names.push(key);
// I placed alert the check the value key.
marks.push(parseInt(val.Science));
});
});
alert(names.toString()); // names is found blank
alert(marks.toString());
当我最后检查数组时。发现数组为空白。为什么 getJSON 出现这种错误行为?我在每个中放置了警报并检查了值。它返回正确的值。
【问题讨论】:
-
如果你只使用 alert(names) 和 alert(marks) 会怎样?不使用 toString() 函数
-
数组不能像 alert(names) 一样打印
标签: javascript jquery json jsonp