简体   繁体   English

在javascript中以特定格式解析Json Array对象

[英]Parsing Json Array Object in specific format in javascript

My JSON is: 我的JSON是:

var json = '{"name":"GeoFence","coordinate":[[1,2],[3,4],[5,6]]}',
obj = JSON.parse(json);
alert(obj.coordinate);

But i need to set the values of coordinates as follows: 但是我需要按如下所示设置坐标值:

var myTrip=[new google.maps.LatLng(1,2),new google.maps.LatLng(3,4),new google.maps.LatLng(5,6)];

Traverse the coordinates and create a new object for each, and add that to myTrip : 遍历坐标并为每个坐标创建一个新对象,并将其添加到myTrip

var myTrip = [];

for(var i=0; i < obj.coordinate.length; i++) {
    myTrip.push(new google.maps.LatLng(obj.coordinate[i][0],obj.coordinate[i][1]))
}

How about this: 这个怎么样:

var json = '{"name":"GeoFence","coordinate":[[1,2],[3,4],[5,6]]}',
var obj = JSON.parse(json);
var myTrip = [];
for (var i = 0; i < obj.coordinate.length; i++) {
    myTrip[i] = new google.maps.LatLng(obj.coordinate[i][0], obj.coordinate[i][1]);
}

You need to iterate over the collection of the coordinates: 您需要遍历坐标集合:

var myTrip = getCoordinate ();

var getCoordinate = function(){
        var ret = [];

        for(var i = 0, length = coordinate.length; i < length; i++){
           ret.push(new google.maps.LatLng(coordinate[i][0], coordinate[i][1]))
        }

       return ret;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM