将JSON对象转换为特定格式

时间:2015-07-16 14:12:46

标签: javascript jquery json

我试图从Json响应中获取此格式的locations

 var locations = [
        ['Bondi Beach', -33.890542, 151.274856, 4],
        ['Coogee Beach', -33.923036, 151.259052, 5],
        ['Cronulla Beach', -34.028249, 151.157507, 3],
        ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
        ['Maroubra Beach', -33.950198, 151.259302, 1]
      ];

这是我的Json回复:

[{"Name":"Bondi Beach","Longitute":-33.890542,"Latitute":151.274856,"Position":1},{"Name":"Coogee Beach","Longitute":-33.923036,"Latitute":151.259052,"Position":2}]

目前我正在做以下事情,但转换时遇到了问题

 var locations;
      var myArray;
      $.getJSON(
       "/top3/DesktopModules/LBSecurity/API/ModuleTask/GetProduct?ProductID=1",
      function (result) {
          var parsed = jQuery.parseJSON(result);
         //Problem is here
         myArray = parsed.map(function (e) {
              return [e.Name, e.Longitute, e.Latitute, e.Position];
          });
         myArray = parsed;  
      });
 locations = myArray;

任何想法我做错了什么?

编辑#1 - 完整代码

<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<div id="themap" style="width: 500px; height: 400px;"></div>

  <script type="text/javascript">
      var locations;
      var myArray;
      $.getJSON(
       "/top3/DesktopModules/LBSecurity/API/ModuleTask/GetProduct?UserID=1",
      function (result) {
          var parsed = jQuery.parseJSON(result);
          myArray = parsed.map(function (e) {
              return [e.Name, e.Longitute, e.Latitute, e.Position];
          });

          //Place this inside the callback function
          locations = myArray;        
      });

      //var locations = [
      //  ['Bondi Beach', -33.890542, 151.274856, 4],
      //  ['Coogee Beach', -33.923036, 151.259052, 5],
      //  ['Cronulla Beach', -34.028249, 151.157507, 3],
      //  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      //  ['Maroubra Beach', -33.950198, 151.259302, 1]
      //];

      var map = new google.maps.Map(document.getElementById('themap'), {
          zoom: 10,
          center: new google.maps.LatLng(-33.92, 151.25),
          mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      var infowindow = new google.maps.InfoWindow();

      var marker, i;

      for (i = 0; i < locations.length; i++) {
          marker = new google.maps.Marker({
              position: new google.maps.LatLng(locations[i][1], locations[i][2]),
              map: map
          });

          google.maps.event.addListener(marker, 'click', (function (marker, i) {
              return function () {
                  infowindow.setContent(locations[i][0]);
                  infowindow.open(map, marker);
              }
          })(marker, i));
      }
  </script>

3 个答案:

答案 0 :(得分:1)

您的.map()功能正常(See an example),因此我要指出两件看似不合适的事情。它应该运作良好。

删除了一个语句,并将最后一个赋值放在回调函数中:

var locations;
var myArray;
$.getJSON("/top3/DesktopModules/LBSecurity/API/ModuleTask/GetProduct?ProductID=1",
    function (result) {
        var parsed = jQuery.parseJSON(result);
        myArray = parsed.map(function (e) {
            return [e.Name, e.Longitute, e.Latitute, e.Position];
        });

        //Place this inside the callback function
        locations = myArray;

        /* REMOVE THE LINE: myArray = parsed; */
    }
);

修改

您必须记住,.getJSON()完成后必须执行其他所有操作。这是一个使用单独功能的想法:

var locations;
var myArray;
$.getJSON("/top3/DesktopModules/LBSecurity/API/ModuleTask/GetProduct?ProductID=1",
    function (result) {
        var parsed = jQuery.parseJSON(result);
        myArray = parsed.map(function (e) {
            return [e.Name, e.Longitute, e.Latitute, e.Position];
        });
        locations = myArray;
        MapFunction();
    }
);

function MapFunction() {
  var map = new google.maps.Map(document.getElementById('themap'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  var infowindow = new google.maps.InfoWindow();

  var marker, i;

  for (i = 0; i < locations.length; i++) {
      marker = new google.maps.Marker({
          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          map: map
      });

      google.maps.event.addListener(marker, 'click', (function (marker, i) {
          return function () {
              infowindow.setContent(locations[i][0]);
              infowindow.open(map, marker);
          }
      })(marker, i));
  }
}

答案 1 :(得分:0)

var locations;
var myArray;
$.getJSON("/top3/DesktopModules/LBSecurity/API/ModuleTask/GetProduct?ProductID=1",
function (result) {
var result= [{"Name":"BondiBeach","Longitute":-33.890542,"Latitute":151.274856,"Position":1},{"Name":"Coogee Beach","Longitute":-33.923036,"Latitute":151.259052,"Position":2}]
var parsed = jQuery.parseJSON(result);
 myArray = parsed.map(function (e) {
 return [e.Name, e.Longitute, e.Latitute, e.Position];
 });
  locations = myArray;
}
);
  • 尝试将您的JSON存储在结果变量中。然后在Parse JSON方法中传递结果

答案 2 :(得分:0)

您可以使用JSON.parse with reviver function

喜欢

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<th>
  <div class="btn-group">
    <button type="button" class="btn btn-default btn-md" ng-click="sort = method1">Numbers</button>
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <span class="caret"></span>
      <span class="sr-only">Toggle Dropdown</span>

    </button>
    <ul class="dropdown-menu">
      <li><a ng-click="sort = method1" class="btn btn-default btn-md"> Sorting Method 1 </a>

      </li>
      <li><a ng-click="sort = method2" class="btn btn-default btn-md"> Sorting Method 2 </a>

      </li>
    </ul>
  </div>
</th>

locations = JSON.parse(result, function(k,v){ if(k && isFinite(Number(k))) return [v.Name, v.Longitute, v.Latitute, v.Position]; return v; }); 得到

'[{"Name":"Bondi Beach","Longitute":-33.890542,"Latitute":151.274856,"Position":1},{"Name":"Coogee Beach","Longitute":-33.923036,"Latitute":151.259052,"Position":2}]'