JSON 数据格式不正确:从 mysql 获取


JSON data not in proper format : fetching from mysql

<?php
    require 'dbinfo.php'; 
    try {
        $db = new PDO($dsn, $username, $password);
        $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
        $sth = $db->query("SELECT * FROM user_tracks");
        $loc = $sth->fetchAll();    
        $locations = array_values($loc);
        echo json_encode( array("user"=>( $locations )));
    } catch (Exception $e) {
      echo $e->getMessage();
    }
?>

代码应返回:

{"user":[{"id":"1","Latitude":"12.9555033333","Longitude":"80.2461883333","Time":"06:32:57","Date":"2012-03-13","Speed":"0","Course":"183.92"},{...},{....}]}

当它返回时:

{"user":[{"id":"1","0":"1","Latitude":"12.9555033333","1":"12.9555033333","Longitude":"80.2461883333","2":"80.2461883333","Time":"06:32:57","3":"06:32:57","Date":"2012-03-13","4":"2012-03-13","Speed":"0","5":"0","Course":"183.92","6":"183.92"},{...},{....}]}

我不确定发生了什么...问题出在哪里?

提前感谢!

默认情况下,

fetchAll(( 会从查询结果中返回(注意 'fetch_style' 参数注释/注释(字符串和数字键控数据。如果你只想要字符串版本,你必须做

$loc = $sth->fetchAll( PDO::FETCH_CLASS );