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

时间:2013-10-01 07:02:03

标签: javascript php mysql arrays json

我不知道如何使用我的JSON JavaScript代码来使用PHP。我假设我需要它将每行解析成一个主数组,为每个数组分配一个从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 CODE:

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

我在控制台中收到错误,而我父亲现在输出

[{ “名称”: “TEST2”, “LAT”: “39.8441792”, “LNG”: “ - 105.104921”, “速度”: “坏”, “高度”: “dontCare项”, “距离”: “无所谓”},{ “名”: “测试”, “LAT”: “39.729431999999996”, “LNG”: “ - 104.831919”, “速度”: “速度”, “高度”: “海拔”, “距离” : “距离”},{ “名称”: “grant3”, “LAT”: “39.729431999999996”, “LNG”: “ - 104.831919”, “速度”: “速度”, “高度”: “高度”,“距离“:” 距离 “},{” 名称 “:” grant2" , “LAT”: “test34”, “LNG”: “test34”, “速度”: “速度”, “高度”: “高度”,“距离“:” 距离 “},{” 名称 “:” 许可 “ ”LAT“: ”39.729431999999996“, ”LNG“: ” - 104.831919“, ”速度“: ”速度“, ”高度“: ”高度“,”距离 “:” 距离 “},{” 名称 “:” “ ”LAT“: ”39.75198511“, ”LNG“: ” - 104.85021166“, ”速度“: ”速度“, ”高度“: ”高度“,”距离 “:” 距离 “}] [{” 名称 “:” TEST2" , “LAT”: “39.8441792”, “LNG”: “ - 105.104921”, “速度”: “坏”, “高度”: “dontCare项” , “距离”: “无论”},{ “名”: “测试”, “LAT”: “39.729431999999996”, “LNG”: “ - 104.831919”, “速度”: “速度”, “高度”:“高原”, “距离”: “距离”},{ “名称”: “grant3”, “LAT”: “39.729431999999996”, “LNG”: “-104.831919”, “速度”: “速度”, “高度”: “高度”, “距离”: “距离”},{ “名称”: “grant2”, “LAT”: “test34”, “LNG” : “test34”, “速度”: “速度”, “高度”: “高度”, “距离”: “距离”},{ “名称”: “许可”, “LAT”: “39.729431999999996”, “LNG” : “ - 104.831919”, “速度”: “速度”, “高度”: “高度”, “距离”: “距离”},{ “名称”: “”, “LAT”: “39.75198511”, “LNG” : “ - 104.85021166”, “速度”: “速度”, “高度”: “高度”, “距离”: “距离”}]

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

consays:

[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"

2 个答案:

答案 0 :(得分:3)

看起来你的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:?, ... },
//    ...
// ]

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

希望这有帮助!

答案 1 :(得分:0)

您应该将所有细节都推送到数组中,然后将该数组编码为json字符串。您可以使用以下代码实现这一目标:

$details_array = array();
while($row = mysql_fetch_array($query))
{
array_push($details_array, $row);//pushes all the fetched row data into $details_array
}
echo json_encode($details_array);