Javascript 通过json数据的Jquery post循环
我正在尝试循环我在执行jquery.post时收到的数据,但是它显示的计数比实际值高得多。数据长度是多少Javascript 通过json数据的Jquery post循环,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我正在尝试循环我在执行jquery.post时收到的数据,但是它显示的计数比实际值高得多。数据长度是多少 .done(function( data ) { console.log(data); console.log(data.length); }); 以下是控制台日志: [{"lat":33.115868,"lng":-117.186295},{"lat":33.117237,"lng":-117.186295},{"lat":33.111866,"lng":-117.18629
.done(function( data ) {
console.log(data);
console.log(data.length);
});
以下是控制台日志:
[{"lat":33.115868,"lng":-117.186295},{"lat":33.117237,"lng":-117.186295},{"lat":33.111866,"lng":-117.186295}]
109
虽然看起来像JSON,但从服务器接收的数据是字符串。要将其转换为JSON,请在迭代之前使用 使用 从字符串中获取JSON对象
或者在配置中使用
dataType:'json'
。您需要使用json.parse
或$.parseJSON
解析编码数据
var ndata=JSON.parse(data);
console.log(ndata.length);
当前您正在读取
json字符串的长度。将数据类型指定为json
,然后jquery将解析json字符串
,并返回javascript对象。
var ndata=JSON.parse(data);
console.log(ndata.length);