JSON.parse() 不适用于变量类型字符串,但如果直接使用,则可以使用其内容

我使用一个简单的代码从 php 页面获取一个字符串,我多次使用该代码从 php 文件中获取字符串响应。这是我第一次尝试将此结果转换为 json 对象(或数组)。


当我在响应上使用 JSON.parse 时,我收到如下所示的错误。如果我作为 JSON.parse() 的参数传递我已控制台日志的文本(即在传递给 javascript 时从控制台复制),它可以完美地工作。



    xmlhttp=new XMLHttpRequest();

  } else {

    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }

  xmlhttp.onreadystatechange=function() {

    if (this.readyState==4 && this.status==200) {

        var result = this.responseText;


        resultsObj = JSON.parse(result);

  }


php代码如下:


<?php

if (!session_id()) {

    @session_start();

}

header('Content-Type: text/json');

require 'controlleur/connexionDB.php';

$sql = "SELECT * FROM depart";


$result = $conn->query($sql);

$liste .= "[";

$compteur = 0;


while ($row = $result->fetch()) {

    if ($compteur != 0) {

        $liste .= ", ";

    }

    $liste .= "{ \"idDepart\" : \"$row->idDepart\", \"idCircuit\" : \"$row->idCircuit\", \"dateDebut\" : \"$row->dateDebut\", \"nbPlaces\" : \"$row->nbPlaces\", \"prix\" : \"$row->prix\", \"titrePromotion\" : \"$row->titrePromotion\", \"rabais\" : \"$row->rabais\" }";

    $compteur++;

}

$liste .= "]";

$reponse = $liste;


echo $reponse;

我收到此错误:SyntaxError: JSON.parse: 第 1 行第 1 列的意外字符


控制台中变量“result”的结果是这样的:


'[

{ "idDepart" : "1", "idCircuit" : "5", "dateDebut" : "2019-06-02", "nbPlaces" : "30", "prix" : "4000", "titrePromotion" : "vfv", "rabais" : "10" }, 

{ "idDepart" : "2", "idCircuit" : "5", "dateDebut" : "2019-06-10", "nbPlaces" : "30", "prix" : "6000", "titrePromotion" : "ded", "rabais" : "4" }, 

{ "idDepart" : "3", "idCircuit" : "5", "dateDebut" : "2019-07-02", "nbPlaces" : "30", "prix" : "7000", "titrePromotion" : "ded", "rabais" : "6" }

]'


犯罪嫌疑人X
浏览 234回答 3
3回答

茅侃侃

您不应该尝试通过操作字符串来创建 json,而是使用内部json_encode方法。$result = $conn->query($sql);$response = [];while ($row = $result->fetch()) {&nbsp; &nbsp; $response[] = [&nbsp; &nbsp; &nbsp; &nbsp; 'idDepart' => $row->idDepart,&nbsp; &nbsp; &nbsp; &nbsp; 'idCircuit' => $row->idCircuit,&nbsp; &nbsp; &nbsp; &nbsp; 'dateDebut' => $row->dateDebut,&nbsp; &nbsp; &nbsp; &nbsp; 'nbPlaces' => $row->nbPlaces,&nbsp; &nbsp; &nbsp; &nbsp; 'prix' => $row->prix,&nbsp; &nbsp; &nbsp; &nbsp; 'titrePromotion' => $row->titrePromotion,&nbsp; &nbsp; &nbsp; &nbsp; 'rabais' => $row->rabais&nbsp; &nbsp; ];}echo json_encode($response);像这样,你 100% 肯定有:如果您的输入无法编码,则出现 PHP 错误。显示有效的 json。- - 编辑 - -我看到你用法语编码,注意特殊字符(如é à ...)。你应该到处都有 UTF-8 编码。你的 php.ini你的 PDO 连接您的数据库字符集、表和字段。$dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf-8", $dbUser, $dbPass);&nbsp;$dbHandle->exec("SET CHARACTER SET utf8");

BIG阳

您可以尝试将 php 脚本简化为这个吗?<?phpif (!session_id()) {&nbsp; &nbsp; @session_start();}header('Content-Type: application/json');require 'controlleur/connexionDB.php';$sql = "SELECT * FROM depart";$result = $conn->query($sql);$liste = array();while($row = $result->fetch()){&nbsp; $list[] = [&nbsp; &nbsp; "idDepart" => $row->idDepart,&nbsp; &nbsp; "idCircuit" => $row->idCircuit,&nbsp; &nbsp; "dateDebut" => $row->dateDebut,&nbsp;&nbsp; &nbsp; "nbPlaces" => $row->nbPlaces,&nbsp;&nbsp; &nbsp; "prix" => $row->prix,&nbsp;&nbsp; &nbsp; "titrePromotion" => $row->titrePromotion,&nbsp;&nbsp; &nbsp; "rabais" => $row->rabais&nbsp; ];}echo json_encode($liste);我使用json_encode()而不是手动编写字符串,我认为这可以更好地避免拼写错误。

冉冉说

确保您在 php 脚本上正确设置了标题<?php&nbsp;header('Content-Type:&nbsp;text/json');
打开App,查看更多内容
随时随地看视频慕课网APP