0

下面是我的 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);
    }
  });
 });

那么如何将“纬度”和“经度”绘制成双维数组?

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

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]
];

提前感谢:)

4

3 回答 3

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)]);
    }
  });
});
于 2013-09-02T14:07:38.523 回答
0

也许用这个

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

    // item.Explore etc etc

});
于 2013-09-02T14:08:47.197 回答
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
});
于 2013-09-02T14:09:15.953 回答