我在代码中使用了ajax,它工作得很好,并给了我我想要的json或数组作为输出。 我使用的代码是,
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://map_ajax_control.php",false);
xmlhttp.send();
var test = xmlhttp.responseText;
alert(test);
这个测试变量给我 json/array。
我想获取在 JavaScript 数组的测试变量中收到的数据。
问题是,如何解码 javascript 数组中的 json 数据? 我使用的代码为,
var output = new Array();
output = json_decode(xmlhttp.responseText);
但是这段代码没有给我任何输出。
我该如何做这两件事?
最佳答案
大多数浏览器支持JSON.parse() 。它的用法很简单:
obj = JSON.parse(xmlhttp.responseText);
alert(obj.length);
对于不支持的浏览器,您可以使用 json2.js 来实现它.
关于javascript - 如何将 json/array 从 ajax responseText 转换为 javascript 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9074807/