为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
犯罪嫌疑人X 2021-06-17 15:32:00
我使用一个简单的代码从 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代码如下:<?phpif (!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" }]'
查看完整描述

3 回答

?
茅侃侃

TA贡献1842条经验 获得超21个赞

您不应该尝试通过操作字符串来创建 json,而是使用内部json_encode方法。


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

$response = [];

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

    $response[] = [

        'idDepart' => $row->idDepart,

        'idCircuit' => $row->idCircuit,

        'dateDebut' => $row->dateDebut,

        'nbPlaces' => $row->nbPlaces,

        'prix' => $row->prix,

        'titrePromotion' => $row->titrePromotion,

        'rabais' => $row->rabais

    ];

}


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); $dbHandle->exec("SET CHARACTER SET utf8");


查看完整回答
反对 回复 2021-06-25
?
BIG阳

TA贡献1859条经验 获得超6个赞

您可以尝试将 php 脚本简化为这个吗?


<?php

if (!session_id()) {

    @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()){

  $list[] = [

    "idDepart" => $row->idDepart,

    "idCircuit" => $row->idCircuit,

    "dateDebut" => $row->dateDebut, 

    "nbPlaces" => $row->nbPlaces, 

    "prix" => $row->prix, 

    "titrePromotion" => $row->titrePromotion, 

    "rabais" => $row->rabais

  ];

}


echo json_encode($liste);

我使用json_encode()而不是手动编写字符串,我认为这可以更好地避免拼写错误。


查看完整回答
反对 回复 2021-06-25
?
冉冉说

TA贡献1877条经验 获得超1个赞

确保您在 php 脚本上正确设置了标题

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


查看完整回答
反对 回复 2021-06-25
  • 3 回答
  • 0 关注
  • 233 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信