Javascript json_编码一个数组,但输出为字符串

Javascript json_编码一个数组,但输出为字符串,javascript,php,arrays,json,Javascript,Php,Arrays,Json,我试图对一个php文件进行ajax调用,该文件将生成一个数组,将其编码为json,然后对数组中的第一项进行console.log。目前,我的主页上有以下内容: <script> window.setInterval(function ajaxcall(){ $.ajax({url: "functions/displayPostsDynamic.php", success: function(da

我试图对一个php文件进行ajax调用,该文件将生成一个数组,将其编码为json,然后对数组中的第一项进行console.log。目前,我的主页上有以下内容:

<script>                

window.setInterval(function ajaxcall(){
            $.ajax({url: "functions/displayPostsDynamic.php",
                success: function(data) 
                {
                console.log(data[0]);
                }
            });
        }, 1000);
</script>                   
因此,最重要的是,它连接到sql数据库,$count是表中的入口数。因此,对于每个条目,我想将所有详细信息添加到一个数组中,最后,将每个条目推到一个大数组中,以便可以将其传输回主页。因此,如果我在发送前打印出responseArray,它会打印出以下内容:

 Array ( 
    [0] => Array ( 
        [0] => Array ( 
            [post_id] => 117 
            [user_id] => 59 
            [post] => lol 
            [date] => 2017-03-26 18:36:21 
            [votes_down] => 2 
            [votes_up] => 1 
        ) 
        [1] => Array ( 
            [post_id] => 104 
            [user_id] => 46 
            [post] => hi from player8 
            [date] => 2017-03-23 22:19:10 
            [votes_down] => 19 
            [votes_up] => 17 
        ) 
    ) 
) 

表中只有2个条目,因此工作正常。运行控制台时返回主页打印出
[
作为接收的字符串的第一个字符,而不是数组的第一项。有人知道如何在返回主页时传输数组,甚至将字符串转换为数组吗?感谢您的帮助

在回显之前添加以下内容:

header('Content-Type: application/json');
echo json_encode($responseArray);
我在代码中注意到了什么,以防您怀疑数据中奇怪地充满了深层数组

$responseArray = []; // initialize array
if ($count > 0) //If there is more than one post
{
    while($row = $result->fetch_assoc()) //For every post, display each one
    {
        $currentPost = ['post_id' => $row["post_id"],'user_id' => $row["user_id"], 'post' => $row["post"], 'date' => $row["date"],'votes_down' => $row["votes_down"],
            'votes_up' => $row["votes_up"]];
        array_push($responseArray, $currentPost); // probably just want to add the post not initialize an array and put the post into that
    }
}

$conn->close();

header('Content-Type: application/json');
echo json_encode($responseArray); // probably dont need that array init here either

在回显之前添加以下内容:

header('Content-Type: application/json');
echo json_encode($responseArray);
我在代码中注意到了什么,以防您怀疑数据中奇怪地充满了深层数组

$responseArray = []; // initialize array
if ($count > 0) //If there is more than one post
{
    while($row = $result->fetch_assoc()) //For every post, display each one
    {
        $currentPost = ['post_id' => $row["post_id"],'user_id' => $row["user_id"], 'post' => $row["post"], 'date' => $row["date"],'votes_down' => $row["votes_down"],
            'votes_up' => $row["votes_up"]];
        array_push($responseArray, $currentPost); // probably just want to add the post not initialize an array and put the post into that
    }
}

$conn->close();

header('Content-Type: application/json');
echo json_encode($responseArray); // probably dont need that array init here either

您的数据像字符串一样处理,只需在jquery ajax调用中设置数据类型:

$.ajax({
   url: "functions/displayPostsDynamic.php",
   dataType: "json",
也可以使用JSON.parse:

success: function(data) 
{
   data = JSON.parse(data);
   console.log(data[0]);
}

您的数据像字符串一样处理,只需在jquery ajax调用中设置数据类型:

$.ajax({
   url: "functions/displayPostsDynamic.php",
   dataType: "json",
也可以使用JSON.parse:

success: function(data) 
{
   data = JSON.parse(data);
   console.log(data[0]);
}

请不要破坏你的问题。请不要破坏你的问题。