如何在Javascript中将json编码的PHP数组转换为数组?

时间:2013-08-17 08:13:15

标签: php javascript ajax arrays json

我从PHP文件中使用AJAX获取JSON编码数组,但在JavaScript中我需要将其用作数组,如何在Javascript中创建数组?

  

我的AJAX调用PHP文件:

  $.ajax({
    type:"POST",
    url:"ajaxfetch.php",
    success:function(result){
            alert(result);
             }
    });
  

ARRAY在PHP文件中创建:

Array
(
    [0] => Array
        (
            [id] => 4
            [deviceID] => xyz123
            [latitude] =>  -33.80010128657071
            [longitude] => 151.28747820854187
            [altitude] => 34.78788787
            [createdDate] => 2013-08-15 23:00:00
            [delete] => 0
        )

    [1] => Array
        (
            [id] => 5
            [deviceID] => jdkfhjskh344
            [latitude] => -33.950198
            [longitude] => 151.259302
            [altitude] => 76.44455
            [createdDate] => 2013-08-15 21:50:42
            [delete] => 0
        )

    [2] => Array
        (
            [id] => 1
            [deviceID] => abc223
            [latitude] => -33.890542
            [longitude] => 151.274856
            [altitude] => 21.4454455
            [createdDate] => 2013-08-15 20:00:00
            [delete] => 0
        )

)

我json用PHP编码了这个数组,但是AJAX检索它并以字符串输出

  

ABOVE ARRAY json编码如下:

$data = array();
$data = $locate_obj->getAllDeviceLocation();


echo json_encode($data);
  

输出json_encode

[{"id":"4","deviceID":"xyz123","latitude":" -33.80010128657071","longitude":"151.28747820854187","altitude":"34.78788787","createdDate":"2013-08-15 23:00:00","delete":"0"},{"id":"5","deviceID":"jdkfhjskh344","latitude":"-33.950198","longitude":"151.259302","altitude":"76.44455","createdDate":"2013-08-15 21:50:42","delete":"0"},{"id":"1","deviceID":"abc223","latitude":"-33.890542","longitude":"151.274856","altitude":"21.4454455","createdDate":"2013-08-15 20:00:00","delete":"0"}]

我正在寻找可以在Javascript中使用我在ajax响应中收到的输出创建数组的方式,这样我就可以得到一个格式数组:

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

4 个答案:

答案 0 :(得分:11)

这个问题有三个解决方案:

如果要修改客户端的结构,请查看Access / process (nested) objects, arrays or JSON

答案 1 :(得分:4)

http://api.jquery.com/jQuery.getJSON/

$.getJSON('ajaxfetch.php', function(data) {
  var locations = []; 

  $.each(data, function(key, val) {
    locations[val.deviceID] = [];
    locations[val.deviceID].push(val.id);
    locations[val.deviceID].push(val.latitude);
    locations[val.deviceID].push(val.longitude);
  });
});

不是100%经过测试,但它是沿着正确的路线。不确定从何处获取位置名称,因为它不在数组中,因此我使用了deviceID。使用getJSON可以让您的生活更轻松。

答案 2 :(得分:0)

确保您的输出是有效的JSON,然后在jQuery AJAX请求中指定dataType: "json"

$.ajax({
    type: "POST",
    url: "ajaxfetch.php",
    dataType: "json",
    success: function (result) {
        console.log(result); //Now a JSON object
    }
});

答案 3 :(得分:-1)

var locations = [];
$.ajax({
    type: "POST",
    url: "ajaxfetch.php",
    dataType: "json",
    success: function (result) {
       result.forEach(function(loc) { locations.push(new Array(loc.deviceID, loc.latitude, loc.longitude, loc.id)) });
       // locations is now in desired format (except non-existent place name)
    }
});