【问题标题】:Merge JSON values into a variable or string将 JSON 值合并到变量或字符串中
【发布时间】:2013-04-12 04:05:19
【问题描述】:

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"}} 
]});

});

【问题讨论】:

  • 将纬度和经度值合并到网址中是什么意思?
  • nodes 合并后会发生什么?除了title之外,每个node都包含不同的数据。
  • 当节点合并时,它只是将所有信息放在相同的值中。我不能将所有纬度和经度都放在一个值中。我需要彼此跟随的纬度和经度。纬度,经度,纬度,经度。我不能有 lat,lat,lat,lat 或 long,long,long,long

标签: javascript jquery json merge jsonp


【解决方案1】:

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

例子:

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

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

【讨论】:

    【解决方案2】:

    您可以尝试做这样的事情,其中​​ 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之前,需要去掉最后一个逗号...

    【讨论】:

      【解决方案3】:

      使用 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');
      

      【讨论】:

      • 太棒了。这可行,但如果有更多具有不同标题的节点呢?
      • @Justin 然后让它成为一个函数。传入节点标题,并将'180' 更改为您想要的任何内容。
      • 嗯,是的。我已经在函数中获得了传递给它的标题,但它返回给我的只是逗号 (,,,,,,,)。也许我需要更好地解释我在做什么?
      • 告诉我字符串需要的样子。我会告诉你怎么做。至于只得到逗号;我不知道。它至少应该返回undefined。另外,例如,here's the string at workin this jsFiddle
      • 我正在解析 JSON 以检查标题 onclick,然后合并所有值并显示它们。这就是我为地址值所做的工作。我只需要你的 JS 在点击时所做的工作。 jsFiddle
      【解决方案4】:

      我必须创建一个函数来合并地址和纬度和经度。查看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
      

      }
      //});

      【讨论】:

        猜你喜欢
        • 2010-10-07
        • 1970-01-01
        • 1970-01-01
        • 2017-04-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多