javascript - 删除 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"}
解决方案
如果您需要此输出,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);
希望这可以帮助。
推荐阅读
- python - 如何为字典中的键分配多个值
- excel - 将单元格中的信息插入代码
- google-sheets - 计算 SUM() 或类似函数时将跳过一列的公式
- r - 带有下拉选择的 R 闪亮可编辑表
- ios - iOS 14.2+ MaterialComponents 密码字段显示空白键盘
- javascript - 当要排序的值是计算结果时如何在Vue中对列进行排序
- python - 获取重复索引列表的列表
- node.js - 尽管 buildpack 在本地的 Docker 中构建和运行良好,但 Google Cloud Functions 有时找不到 @google-cloud/* 依赖项
- google-bigquery - Bigquery Stackoverflow - 按类别查询已关闭的问题
- assembly - 内部重定位(类型:OFFST_IMM)与汇编器中的寄存器变量