首页 > 解决方案 > 删除 json_encode 中的双引号

问题描述

我从我的数据库中提取了一些数据。工作完美。但我想删除 JSON 中的双引号。这是我的代码;

$sql = "SELECT id, instructions, quiz_question, correct, wrong, wrong1, wrong2 FROM student_quiz WHERE subject = 'SOCIAL STUDIES' AND type = 'challenge'";

$results = $pdo->query($sql);
$results->setFetchMode(PDO::FETCH_ASSOC);

$json = [];

while($row = $results->fetch()) {
    $choices = [
        $row['correct'],
        $row['wrong'],
        $row['wrong1'],
        $row['wrong2'],
    ];

    // shuffle the current choices so the 1st item is not always obviously correct
    shuffle($choices);

    $json[] = [
        'question' => $row['quiz_question'],
        'choices' => $choices,
        'correctAnswer' => $row['correct'],
    ];
}

echo json_encode($json);

正在回显这样的数据;

{"question":"Who said this statement \"Ghana your beloved country is free for ever\"?
<\/p>","choices":["Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"],"correctAnswer":"Kwame Nkrumah"}

但我想要这样:

{question:"Who said this statement \"Ghana your beloved country is free for ever\"?
<\/p>",choices :["Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"],correctAnswer :"Kwame Nkrumah"}

标签: javascriptphpjson

解决方案


如果您需要此输出,PHP 给出的输出是正确的:

{question:"Who said this statement \"Ghana your beloved country is free for ever\"?
<\/p>",choices :["Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"],correctAnswer :"Kwame Nkrumah"}

在javascript中,尝试使用..

var json = JSON.parse(response);

在 php 中尝试使用

$json = json_decode($json);

希望这可以帮助。


推荐阅读