![](/img/trans.png)
[英]how convert string arrays of arrays into normal arrays without using JSON.parse
[英]How to parse JSON of String Arrays
我有一个ajax代码,它返回只是字符串数组(无键/值对)的JSON数据。
function loadData() {
$.ajax({
type: 'GET',
url: apiURL,
dataType: "json",
timeout: 2000,
success: onLoadData
};
function onLoadData(data) {
console.log(data);
var arr = JSON.parse(data);
//var arr = jQuery.parseJSON(data); this also fails.
alert(arr[0]);
};
console.log()
输出为= ["one", "two", "three"]
但是JSON.parse()
给出错误为:
未捕获到的SyntaxError:意外令牌o
只需remove JSON.parse(data)
如果它已经是一个数组。 您会看到您的alert
电话应该可以正常工作。
做就是了:
alert(data[0]) // and remove everything else in that callback.
您已经可以使用dataType : "json"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.