gpt4 book ai didi

javascript - 如何从数据库行创建有效的 JSON

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:38 24 4
gpt4 key购买 nike

我不知道如何编写 PHP 来处理我的 JSON JavaScript 代码。我假设我需要它将每一行解析为一个主数组,该数组为每一行分配一个从 1 开始到无穷大的唯一值键。但是,老实说,我已经看过了,但我不知道该怎么做。另外,如果您发现映射添加标记有任何问题,请告诉我。

PHP 代码:

 <?php
include 'dbconnect.php';

$result = mysql_query("SELECT * FROM coords ORDER BY name DESC") or die ("Could
not query");
while($row = mysql_fetch_array($result)) {
$r[] = array(
"name" => $row['name'],
"lat" => $row['lat'],
"lng" => $row['lng'],
"speed" => $row['speed'],
"altitude" => $row['altitude'],
"distance" => $row['distance']
);
}

$encoded = json_encode($r);
echo $encoded;

exit($encoded);

mysql_close($conn);

?>

JavaScript 代码:

  var usermarker;
var markloc;

function deleteUserOverlay() {
if (usermarker) {
usermarker.setMap(null);
}
}

function calluserlocation(){
console.log('calluserlocation fires');
$.ajax( {
url: "getdata.php",
type: "GET",
dataType: "json",
success: function(data) { for (var i = 0; i < data.length; i++) { markloc = new google.maps.LatLng(data[i].b, data[i].c); adddata(markloc); } }, error: function(data) { console.log( "error" ); } });
console.log("sucessful run of function");

}
function adddata(markloc){
marker = new google.maps.Marker({
position: markloc,
icon: 'http://www.wolfdoginfo.net/app/cropcircles.png',
map: map
});
deleteUserOverlay();
usermarker = marker;
}

我在控制台中收到错误,我爸爸现在输出这样

[{"name":"test2","lat":"39.8441792","lng":"-105.104921","speed":"bad","altitude":"dontcare","distance": "whatever"},{"name":"test","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"grant3","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude":"altitude","distance"":"distance"},{"name":"grant2","lat":"test34","lng":"test34","speed":"speed","altitude":"altitude","distance ":"distance"},{"name":"grant","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"","lat":"39.75198511","lng":"-104.85021166","speed":"speed","altitude":"altitude","distance":"distance"}][{"name":"test2","lat":"39.8441792","lng":"-105.104921","speed":"bad","altitude":"dontcare","distance":"whatever"},{"name":"test","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude":"altitude ","distance":"distance"},{"name":"grant3","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"grant2","lat":"test34","lng":"test34","speed":"speed","altitude":"altitude","distance":"distance"},{"name":"grant","lat":"39.729431999999996","lng":"-104.831919","speed":"speed","altitude": “高度”,“距离”:“距离”},{“名称”:“”,“纬度”:“39.75198511”,“lng”:“-104.85021166”,“速度”:“速度”,“高度”: “高度”,“距离”:“距离”}]

所以现在我的 php 出于某种原因只正确输出了两次但我的 json 代码不工作。我需要为每个条目填充标记。

认为:

[01:57:52.768] GET http://wolfdoginfo.net/app/show/getdata.php [HTTP/1.1 200 OK 86ms]
[01:57:52.705] "calluserlocation fires"
[01:57:52.705] "sucessful run of function"
[01:57:52.826] "error"

最佳答案

看起来您的 PHP 代码返回了无效的 JSON,因为它是一系列 json 字符串,而不是一个组合数组。

while($row = mysql_fetch_array($query)) {
echo json_encode(array( "a" => $row['name'], "b" => $row['lat'], "c" => $row['lng'], "d" => $row['speed'], "e" => $row['altitude'], "f" => $row['distance']));
}
// this code will return this JSON
// { a:?, b:?, c:?, ... }
// { a:?, b:?, c:?, ... }
// ...

你应该将它们组合成一个数组并返回它。

$return = array();
while($row = mysql_fetch_array($query)) {
$return [] = array( "a" => $row['name'], "b" => $row['lat'], "c" => $row['lng'], "d" => $row['speed'], "e" => $row['altitude'], "f" => $row['distance']));
}
echo json_encode($return);
// this code will return
// [
// { a:?, b:?, c:?, ... },
// { a:?, b:?, c:?, ... },
// ...
// ]

这是一个对象数组,而不仅仅是松散的对象。

希望这对您有所帮助!

关于javascript - 如何从数据库行创建有效的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19109941/

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