将JSON值合并到变量或字符串中

时间:2013-04-18 21:12:30

标签: javascript jquery json merge jsonp

I JSON节点具有相同的标题,但每个节点中的纬度和经度值不同。我需要检查相同的标题值,然后将纬度和经度值合并到一个地图API的URL中。我需要它按照纬度,经度,纬度,经度等顺序...我只是不知道该做什么。感谢您提供任何帮助或建议。

JS VAR

<img class="detail_map" src="http://open.mapquestapi.com/staticmap/v4/getplacemap?size=320,240&zoom=15&location=' + data.nodes.Latitude + ',' + data.nodes.Longitude + '&imagetype=jpeg&showicon=blue-1">

JSON对象

var data = fl({
"nodes":[
    {"node":{
        "title":"180","Address":"555 Market St. San Francisco, CA United States See map: Google Maps","
        Latitude":"37.789952","
        Longitude":"-122.400158"}},
    {"node":{
        "title":"180","Address":"Epic Roasthouse (399 Embarcadero) San Francisco, CA United States See map: Google Maps","
        Latitude":"37.797677","
        Longitude":"-122.394339"}},
    {"node":{
        "title":"180","Address":"Mason &amp; California Streets (Nob Hill) San Francisco, CA United States See map: Google Maps","
        Latitude":"37.791556","
        Longitude":"-122.410766"}},
    {"node":{
        "title":"180","Address":"Justin Herman Plaza San Francisco, CA United States See map: Google Maps","
        Latitude":"37.774930","
        Longitude":"-122.419416"}},
    {"node":{
        "title":"180","Address":"200 block Market Street San Francisco, CA United States See map: Google Maps","
        Latitude":"37.793133","
        Longitude":"-122.396560"}} 
]});

});

4 个答案:

答案 0 :(得分:0)

您可以使用$.extend()函数合并两个对象,将替换不同的属性。

示例:

var newData = $.extend(data1, data2);
// Assuming data1, data2 are objects;

如果您需要将字符串JSON转换为对象,请使用$.parseJSON()

答案 1 :(得分:0)

你可以尝试做类似这样的事情,其中​​var url_part用于替换img src中的“data.nodes.Latitude +','+ data.nodes.Longitude”:

var url_part = '';
$.each(data.nodes, function(key,val){
    url_part += val.node.Latitude+","+val.node.Longitude+",";
});

但在使用url_part之前,您需要删除最后一个昏迷...

答案 2 :(得分:0)

使用jQuery的$.map()。比较对象的node.title值,然后从那里返回字符串,如您所料。完成后,在,上加入数组。

var locs = $.map(fl.nodes, function(obj,i){
    return obj.node.title == '180' ? 'latitude='+obj.node.Latitude+'&longitude='+obj.node.Longitude : '';
}).join(',');

会返回lat1,long1,lat2,long2,lat3,long3,lat4,long4,lat5,long5

Working jsFiddle

修改1

添加了对动态图书的支持。

function getNodesByTitle(title){
    return $.map(fl.nodes, function(obj,i){
        return obj.node.title == title ? 'latitude='+obj.node.Latitude+'&longitude='+obj.node.Longitude : '';
    }).join(',');
}

在理论实践中:

var locString = getNodesByTitle('your title here');

答案 3 :(得分:0)

我必须创建一个合并地址和纬度和经度的函数。查看jsFiddle以查看其实际效果。

function fl(data){
//data = JSON.parse(data);
//Array to hold movie titles   
var movieTitles = []; 
//Assign Movies to to movies Array and ensure unique
for (var i=0; i < data.nodes.length; i++)
{
    //Look for the current title in the movieTitles array
    var movieIndex = movieTitles.indexOf(data.nodes[i].node.title);
    if (movieIndex>-1) //If the title already exists
    {
        //Merge all the properties you want here
        movies[movieIndex].Address += ", " + data.nodes[i].node.Address;
        if(!movies[movieIndex].Coords) movies[movieIndex].Coords = [];
        movies[movieIndex].Coords.push(
            data.nodes[i].node.Latitude + "," + data.nodes[i].node.Longitude
        );
    }
    else
    {
        //var address = movies[movieIndex].Address; movies[movieIndex].Address = address.replace(/^\s*/,'');
        //Add movie to movies array
        movies.push(data.nodes[i].node);
        //Add movie title to movieTitles array
        movieTitles.push(data.nodes[i].node.title);

    }
}

displayLinks(); //Load all the links
//showCast(0); //Display details for first item

}
//});