gpt4 book ai didi

javascript - 删除 json_encode 中的双引号

转载 作者:行者123 更新时间:2023-12-01 01:42:47 25 4
gpt4 key购买 nike

我从数据库中提取了一些数据。工作完美。但我想删除 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"}

最佳答案

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);

希望这有帮助。

关于javascript - 删除 json_encode 中的双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52265080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com