Jquery:遍历当前为字符串的嵌套列表

时间:2011-12-20 13:38:47

标签: jquery json

我有以下JSON结构:

"data":[
{
"Status":"",
"Description":"SDH Source: http://www.flickr.com/photos/ssong/5980312175/ Parts of this are highly speculative and probably wrong.",
"Country":"Botswana",
"Route":"Ghanzi - Namibia border",
"__id__":1,
"Operator":"",
"Linestring":"[[21.655687226945439,-21.69807092918915],[21.606544897735219,-21.698981514014829],[21.55814494317222,-21.6948413352827],[21.492934687173641,-21.70172591790476],[21.432925637207919,-21.707152166982251],[21.4108749703861,-21.712328345347341],[21.38092408696798,-21.733997442781551],[21.33297121701732,-21.737244186092511],[21.270621127018181,-21.7492165472962],[21.209296018166889,-21.760407028676049],[21.184802907584931,-21.7673550397231],[21.124295475909431,-21.77495082482249],[21.036772532878931,-21.902107946653949],[21.030922147053559,-21.92655934013332],[21.029823515972719,-21.939804388740249],[21.024525846876742,-21.946069573594141],[20.999362774212379,-21.947996797946232]]"
},
{
"Status":"",
"Description":"SDH Source: http://www.flickr.com/photos/ssong/5980312175/ Parts of this are highly speculative and probably wrong.",
"Country":"Botswana",
"Route":"Main Loop",
"__id__":2,
"Operator":"",
"Linestring":"[[25.868245525590559,-24.659437558460471],[25.839065125037649,-24.636438708648949],[25.8304766089291,-24.63941684823082],[25.764698657054229,-24.652354808830591],[25.66910795259367,-24.65864351524873],[25.621786791268288,-24.690836785084102],[25.525336477796891,-24.688237872812209],[25.439770053803031,-24.762743692863971],[25.388036603028329,-24.834169218942151],[25.34840467511296,-24.907043674808271],[25.295372125100659,-24.95527683356271],[25.041334775867188,-24.824558772828009],[24.881696518620149,-24.718259087968971]]"

} ]

我想迭代JSON并检索每个Linestring值并插入到谷歌地图中。不幸的是,因为Linestring值是一个字符串,我在迭代它时遇到了问题。我尝试使用eval()方法和$ .parseJSON方法没有任何成功,代码似乎不止一次迭代,所以我得到了一个混乱的地图。

function OnLoad() {
  var my_array;
  var coords;
  var points = [];
  $.getJSON(url, function (data) {
   $.each(data.data, function(key,val){
       my_array = eval('(' + data.data[count].Linestring + ')');
       for (j = 0; j<linestring_array[count].length;j++){
    coords = linestring_array[j];
            points.push(new google.maps.LatLng(coords[1], coords[0]));
       }
            }
   });
 });

1 个答案:

答案 0 :(得分:0)

这将帮助您(和其他评论者)出去..

  function OnLoad() {
          var my_array;
          var coords;
          var points = [];
          $.getJSON(url, function (data) {
                 $.each(data, function(key,val){
                        var my_array = $.pareJSON(val.Linestring),points=[];
                        for (var j = 0; j<my_array.length;j++){
                               var coords = my_array[j];

                               //you'll have to check up on the correct usage of google.maps api
                               points.push(new google.maps.LatLng(coords[1], coords[0]));
                        }
                        console.log(points);
                 });
          });
   };