gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:30:54 25 4
gpt4 key购买 nike

<分区>

我正在使用 AJAX 从 PHP 文件中获取一个 JSON 编码的数组,但在 JavaScript 中我需要将它用作一个数组,我如何在 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
)

)

我在 PHP 中对这个数组进行了 json 编码,但 AJAX 检索了它并输出为字符串

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com