【发布时间】:2019-02-15 07:38:36
【问题描述】:
我从我的数据库中提取了一些数据。工作完美。 但我想删除 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"}
【问题讨论】:
-
JSON 要求属性名称用双引号引起来。你为什么要改变它?你想达到什么目的?
-
他说了什么,你不希望这样,因为你无法使用
json_decode对其进行解码,事实上我为另一个问题写了一个解析器,因为他们有 json没有他们无法使用内置函数解析的引号......只是说。 -
您想要的格式称为“JSOL”(JavaScript 对象文字)。如前所述,在 PHP 领域几乎没有解析器和生成器。并且通常应该避免,除非有比“我想要”更好的理由。
-
@Robee 双引号非常适合 JavaScript 对象文字表示法。你不需要摆脱它们。
-
Javascript 应该仍然可以使用引号。 JSON.parse()
标签: javascript php json