如何将json/array从中的ajax responseText转换为javascript数组
how to convert json/array from ajax responseText in to javascript array?
我在代码中使用了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.
试试这个:
var arr = xmlhttp.responseText.Split(',');
如果它不能解决您的问题,那么在php代码中,使用简单的json_encode(your array);
,在javascript上,使用myData= eval("(" + xmlHttp.responseText + ")");
。
我建议你采用这种方法:
在服务器上使用JSON的PHP绑定对要发送的数据进行编码,并使用JSON的Javascript库对其进行解码。作为:
var myObject = eval('(' + myJSONtext + ')');
或
var myObject = JSON.parse(myJSONtext, reviver);
注意:将json2 javascript文件包含到您的解决方案中。。
将值存储在数组中从php到AJAX 的问题
json不过是javascript对象表示法。你只需要按照Sudhir的建议来解析它。您也可以使用jQuery.parseJSON。
为了实现ajax,我强烈建议您使用一些库,最好是jQuery。
http://api.jquery.com/jQuery.ajax/
相关文章:
- 为什么我得到responseText(通过Ajax从Servlet到javaScript)和额外的字符
- 无法从jquery中的函数返回ajax responseText
- ajax responseText验证功能冻结页面内容(async)
- $.ajax():responseText 是正确的,但报告了错误.为什么
- jquery ajax readystate 0 responsetext status 0 statustext er
- If Else Condition with Ajax responseText from PHP
- PHP to JS:打印与 AJAX responseText 兼容的 js 变量(如 PHP echo)
- 在粘贴responseText后,调用Ajax响应文本中存在的Javascript onchange函数
- 如果ajax.responseText包含
- 我可以't使用javascript从android ajax中获取responseText的数据
- Ajax responseText和echo已损坏,返回头文件内容
- 无法访问延迟对象中的jquery ajax responseText
- 如何将json/array从中的ajax responseText转换为javascript数组
- php ajax jquery responseText not returned
- 通过ajax接收php数据作为数组,而不是我的javascript中的responseText
- AJAX responseText字符串比较
- 在.js文件中,我可以在处理ajax responsetext后更新一个变量吗
- 从AJAX函数返回xmlhttp.responseText
- 如何从ajax responseText中获取json/array到javascript数组
- ajax GET请求responseText为空