如何JSON EN codeD PHP数组转换为在Javascript数组?数组、转换为、EN、JSON

2023-09-10 17:36:46 作者:从小帅到大

我取一个JSON连接使用AJAX从PHP文件codeD数组,但在JavaScript中,我需要把它作为一个数组,我怎么能在JavaScript中创建一个数组?

  

我的AJAX调用到PHP文件:

  $。阿贾克斯({
    键入:POST,
    网址:ajaxfetch.php
    成功:函数(结果){
            警报(结果);
             }
    });
 

  

ARRAY创建在PHP文件:

 阵列
(
    [0] =>排列
        (
            [ID] => 4
            [设备ID] => XYZ123
            [纬度] => -33.80010128657071
            [经度] => 151.28747820854187
            [海拔] => 34.78788787
            [createdDate] => 2013年8月15日23:00:00
            [删除] => 0
        )

    [1] =>排列
        (
            [ID] =>五
            [设备ID] => jdkfhjskh344
            [纬度] => -33.950198
            [经度] => 151.259302
            [海拔] => 76.44455
            [createdDate] => 2013年8月15日21点50分42秒
            [删除] => 0
        )

    [2] =>排列
        (
            [ID] => 1
            [设备ID] => abc223
            [纬度] => -33.890542
            [经度] => 151.274856
            [海拔] => 21.4454455
            [createdDate] => 2013年8月15日20:00:00
            [删除] => 0
        )

)
 
package.json 知多少

我的JSON恩codeD这个数组在PHP 但是AJAX检索,并输出字符串中的

  

以上ARRAY JSON EN code,如下所示:

  $数据=阵列();
$数据= $ locate_obj-> getAllDeviceLocation();


回声json_en code($的数据);
 

  

输出json_en code

  [{ID:4,设备ID:XYZ123,纬度: -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秒,删除:0}]
 

我在寻找我可以在JavaScript中创建一个包含有我收到的Ajax响应输出的方式,让我能想出格式的数组:

  VAR位置= [
      ['邦迪海滩,-33.890542,151.274856,4]
      ['Coogee海滩,-33.923036,151.259052 5]
      ['克罗纳拉海滩,-34.028249,151.157507,3]
      ['曼利海滩,-33.80010128657071,151.28747820854187,2]
      ['马鲁巴海滩,-33.950198,151.259302,1]
    ]。
 

解决方案

有三种解决这个问题:

呼叫 JSON.parse 明确并传递响应文本给它的。返回值将是一个JavaScript的数据类型。

设置数据类型:JSON选项,在 $。阿贾克斯 打电话让jQuery的解析JSON你。

设置正确的的响应头JSON 在PHP。 jQuery将探测头和解析JSON,并自动完成。

如果要修改在客户端的结构,看看Access /过程(嵌套)对象,数组或JSON 。

I am fetching a JSON encoded array using AJAX from a PHP file, but in JavaScript I need to use it as an array, how can I create an array in Javascript?

My AJAX call to PHP File:

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

ARRAY Created in PHP File :

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
        )

)

I json encoded this array in PHP but AJAX retrieved it and is outputted in a string.

ABOVE ARRAY json encode as given below:

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


echo json_encode($data);

Output of 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"}]

I'm looking for the way I can create an array in Javascript with the output I recieve in ajax response, so that I can come up with an array of format:

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

解决方案

There are three solution to this problem:

Call JSON.parse explicitly and pass the response text to it. The return value will be a JavaScript data type.

Set the dataType: 'json' option in your $.ajax call so that jQuery parses the JSON for you.

Set the right response headers for JSON in PHP. jQuery will detect the header and parse the JSON automatically as well.

If you want to modify the structure on the client side, have a look at Access / process (nested) objects, arrays or JSON.