[英]iterating through and reading json data
我正在使用ajax从服务器获取json字符串,然后使用eval将其转换为对象。 当我遍历json数据时,我只会得到密钥。 我如何获得钥匙的价值。 这就是我所拥有的:
var jsonobj = eval('(' + xmlhttp.responseText + ')');
for (i in jsonobj){
alert(i);
}
哪个提示按键。 如何获得钥匙的价值?
使用子脚本符号: jsonobj[i]
尝试这个:
var jsonobj = eval('(' + xmlhttp.responseText + ')');
var value;
for (i in jsonobj){
value = jsonobj[i];
}
如果服务器返回JSON,则无需使用eval。 只需指定dataType
,jQuery就会自动为您解析结果:
$.ajax({
url: '/script',
type: 'POST',
dataType: 'json',
success: function(result) {
for (var key in result) {
if (result.hasOwnProperty(key)) {
alert('key: ' + key + ', value: ' + result[key]);
}
}
}
});
var jsonobj = eval('(' + xmlhttp.responseText + ')');
for (i in jsonobj){
alert(jsonobj[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.