如何将Json数据绘制成数组?

时间:2013-09-02 14:04:06

标签: javascript jquery arrays json google-maps

以下是我的JSON格式。

{
"Data": {
"-template": "Parallax",
"Explore": {
  "IslandLife": {
    "TourismLocation": [
      {
        "Title": "Langkawi",
        "Latitude": "6.350000",
        "Longitude": "99.800000",
        "YouTubePlaylistID": "UUnTJRksbHP4O8JSz00_XRQA",
        "VideosList": {
          "YouTubeVideoID": [
            "7HlMpaSspNs",
            "tzeRnbd77HU",
            "VkIAbDIVJCA",
            "ksJ6kwTe9wM"
          ]
        }
      },
      {
        "Title": "Rawa Island",
        "Latitude": "2.520278",
        "Longitude": "103.975833",
        "YouTubePlaylistID": "UUnTJRksbHP4O8JSz00_XRQA",
        "VideosList": { "YouTubeVideoID": "Kx0dUWaALKo" }
      },
      {
        "Title": "Perhentian Island",
        "Latitude": "5.903788",
        "Longitude": "102.753737",
        "YouTubePlaylistID": "UUnTJRksbHP4O8JSz00_XRQA",
        "VideosList": {
          "YouTubeVideoID": [
            "ZpcdGk5Ee0w",
            "TQTDOGpflZY"
          ]
        }
      },
      {
        "Title": "Sabah Marine Park",
        "Latitude": "4.623326",
        "Longitude": "118.719800",
        "YouTubePlaylistID": "UUnTJRksbHP4O8JSz00_XRQA",
        "VideosList": { "YouTubeVideoID": "VCDTEKOqpKg" }
      }
    ]
  }
}
  }
}

并使用以下此功能我将从Json中恢复数据

$.getJSON('json/explore.json', function(data) {
$.each(data, function(key, val) {
    for (var i = 0; i < val.Explore.IslandLife.TourismLocation.length; i++) {
       console.log(val.Explore.IslandLife.TourismLocation[i].Title);
        console.log(val.Explore.IslandLife.TourismLocation[i].Description);
         console.log(val.Explore.IslandLife.TourismLocation[i].Latitude);
         console.log(val.Explore.IslandLife.TourismLocation[i].Longitude);
        console.log(val.Explore.IslandLife.TourismLocation[i].YouTubePlaylistID);
    }
  });
 });

那么如何将“Latitude”和“Longitude”绘制成Double Dimensial数组??

我想要像我这样的数组对象?

var locations = [
['Langkawi', 6.350000, 99.800000, 4],
['Rawa Island', 2.520278, 103.975833, 3],
['Perhentian Island', 5.903788, 102.753737, 2],
['Sabah Marine Park', 4.623326, 118.719800, 1]
];

Thanx in Advance :)

3 个答案:

答案 0 :(得分:0)

var locations = [];

$.getJSON('json/explore.json', function(data) {
$.each(data, function(key, val) {
    for (var i = 0; i < val.Explore.IslandLife.TourismLocation.length; i++) {
       var currLoc = val.Explore.IslandLife.TourismLocation[i];
       locations.push([currLoc.Title, currLoc.Latitude, currLoc.Longitude];
    }
  });
});

console.log(locations);

但是我无法想象你在哪里检索每个数组的4 th 元素:如果它是一个反向计数器,你可能会变成

...
$.each(data, function(key, val) {
    var t = val.Explore.IslandLife.TourismLocation,
        tLen = t.length;

    for (var i = 0; i < tLen; i++) {
       var currLoc = t[i];
       locations.push([currLoc.Title, currLoc.Latitude, currLoc.Longitude, (tLen - i)]);
    }
  });
});

答案 1 :(得分:0)

也许使用这个

$.each($.parseJSON(data), function(i,item)
{

    // item.Explore etc etc

});

答案 2 :(得分:0)

获取数据,然后迭代TourismLocation对象,将值添加到数组中,然后将该数组推送到包含的数组:

$.getJSON('json/explore.json', function(data) {
    var locations = [];
    $.each(data.Data.Explore.IslandLife.TourismLocation, function(key, val) {
        var loc = [val.Title, val.Latitude, val.Longitude];
        locations.push(loc);
    });
    // use "locations" here, async and all
});

另请注意,ajax是异步的,因此您无法在回调函数之外使用新创建的数组,因为它尚不可用。

不确定数组中的第四项是什么,但如果它只是反向迭代的数字,你也可以添加它:

$.getJSON('json/explore.json', function(data) {
    var locations = [],
        num = data.Data.Explore.IslandLife.TourismLocation.length;

    $.each(data.Data.Explore.IslandLife.TourismLocation, function(key, val) {
        var loc = [val.Title, val.Latitude, val.Longitude, num--];
        locations.push(loc);
    });
    // use "locations" here, async and all
});