ajax和json数组中的错误

时间:2013-08-18 05:11:13

标签: ajax arrays json parsing

我要开发一个交互式地图(谷歌地图)。坐标通过json的ajax解析,但是我收到错误

JSON格式

{"Level":[{"route":[{"lat":39.105251,"lgn":26.551727},
{"lat":39.105247125,"lgn":26.551774625}...],"balls":
[{"lat":39.105239375,"lgn":26.551869875},{"lat":39.10524325,"lgn":26.55182225}]},
{"route":[{........},{.....}...],"balls":[{........},{.....}...]}]}

AJAX请求

$.ajax({
   type: "GET",
   url: "getcoordp?q=2",
   dataType: "json",
   cache: false,
   contentType: "application/json",
   success: function(data) {
  $.each(data.Level, function(i,Level){
    $.each(data.route, function(index, route) {
        alert(data.lat)
        });  
    }); 

   }
   });

ERROR

Uncaught TypeError: Cannot read property 'length' of undefined

任何帮助?

2 个答案:

答案 0 :(得分:0)

替换下面的

$.each(data.route, function(index, route)...

使用

$.each(Level.route, function(index, route) {
    alert(route.lat)
});  

这将解决您的问题。

答案 1 :(得分:0)

试试这个

for(i=0;i<data.Level.length;i++){
console.log(data.Level[i].route);
}