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

How to convert json encoded PHP array to an array in Javascript?

本问题已经有最佳答案,请猛点这里访问。

我正在从一个PHP文件中获取一个使用Ajax的JSON编码的数组,但在javascript中,我需要将它用作数组,如何在javascript中创建一个数组?

My AJAX call to PHP File:

1
2
3
4
5
6
7
  $.ajax({
    type:"POST",
    url:"ajaxfetch.php",
    success:function(result){
            alert(result);
             }
    });

ARRAY Created in PHP File :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 encode as given below:

1
2
3
4
5
$data = array();
$data = $locate_obj->getAllDeviceLocation();


echo json_encode($data);

Output of json_encode

1
[{"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响应中接收输出,这样我就可以想出一个格式数组:

1
2
3
4
5
6
7
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.parse并将响应文本传递给它。返回值将是一个javascript数据类型。

  • $.ajax调用中设置dataType: 'json'选项,以便jquery为您解析JSON。

  • 在PHP中为JSON设置正确的响应头。jquery将检测头并自动解析JSON。

如果您想修改客户端的结构,可以查看访问/处理(嵌套)对象、数组或JSON。


1
2
3
4
5
6
7
8
9
10
11
12
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可以让你的生活更轻松。


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

1
2
3
4
5
6
7
8
$.ajax({
    type:"POST",
    url:"ajaxfetch.php",
    dataType:"json",
    success: function (result) {
        console.log(result); //Now a JSON object
    }
});


1
2
3
4
5
6
7
8
9
10
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)
    }
});