使用 PHP json_encode() 和 MySQL 返回一个 JSON 对象以传递给 jQuery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4507366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:14:23  来源:igfitidea点击:

Return a JSON object using PHP json_encode() & MySQL to pass to jQuery function

phpjquerymysqljsongoogle-maps-api-3

提问by JCam

I'm trying to create a json object from MySQL results, but not getting the result I need.

我正在尝试从 MySQL 结果创建一个 json 对象,但没有得到我需要的结果。

Here is the PHP

这是PHP

$json = array();
$result = mysqli_query ($connection, $query);
    echo '['; 

        while($row = mysqli_fetch_array ($result))     
        {
            echo '{';
            echo '"latitude":"'.$row['lat'].'",';
            echo '"longitude":"'.$row['lng'].'",';
            echo '"icon":'.'"./images/'.$row['busColor'].'.png"';
            echo '}';    
        }
        echo ']';

        $jsonstring = json_encode($json);
        echo $jsonstring;

        die(); 

It outputs this

它输出这个

[{"latitude":"39.976257","longitude":"-83.003464","icon":"./images/pink.png"}][]

But I want this

但我想要这个

[{"latitude":"39.976257","longitude":"-83.003464","icon":"./images/pink.png"}]

once I get the result I need to pass the object to a jQuery plugin function if that makes any difference

一旦我得到结果,我需要将对象传递给 jQuery 插件函数,如果这有什么不同的话

$.getJSON('myJsonURL, function(myMarkers){
  $("#map").goMap({
    markers: myMarkers
  });
});

Thanks

谢谢

回答by RabidFire

I guess the correct way to do this would be:

我想这样做的正确方法是:

$json = array();
$result = mysqli_query ($connection, $query);
while($row = mysqli_fetch_array ($result))     
{
    $bus = array(
        'latitude' => $row['lat'],
        'longitude' => $row['lng'],
        'icon' => './images/' . $row['busColor'] . '.png'
    );
    array_push($json, $bus);
}

$jsonstring = json_encode($json);
echo $jsonstring;

die();

回答by cristian

you output your json by hand and then you call json_encode on an empty array() - $json

您手动输出 json,然后在空数组上调用 json_encode() - $json

json_encode() outputs [] on you pass an empty array so your last [] comes from here
$jsonstring = json_encode($json);
echo $jsonstring;

Edit: More about json_encode json_encode php manual

json_encode() 在你传递一个空数组时输出 [] 所以你的最后一个 [] 来自这里 编辑:更多关于 json_encode json_encode php 手册
$jsonstring = json_encode($json);
echo $jsonstring;

回答by Quentin

You start by defining an array.

首先定义一个数组。

You then generate some JSON manually.

然后您手动生成一些 JSON。

You then convert the array to JSON and output it.

然后将数组转换为 JSON 并输出它。

Replace all the echo statements in and at the edge of your while loop with code to generate an associative array containing the data, and then insert it into $json.

用代码替换 while 循环中和边缘的所有 echo 语句以生成包含数据的关联数组,然后将其插入 $json 中。