我从数据库中提取了一些数据。运行良好。 但是我想删除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"}
答案 0 :(得分:2)
如果需要此输出,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);
希望这会有所帮助。
答案 1 :(得分:0)
您可以尝试编码然后像这样解码
$encodeJson = json_encode($json);
print_r(json_decode($encodeJson, true));
如果需要每个偏移量,则可以这样打印,并且不会添加引号:
$decodeJson = json_decode($encodeJson, true);
print $decodeJson['question'];
编辑:下面的代码是注释答案:
$data = array(
"question" => "Who said this statement Ghana your beloved country is free for ever",
"choices" => array("Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"),
"correctAnswer" => "Kwame Nkrumah"
);
$jsonEncode = json_encode($data);
$jsDecode = json_decode($jsonEncode, true);
// a correct json encoded array would output something like this:
{
"question":"Who said this statement Ghana your beloved country is free for ever ?",
"choices":[
"Jack Chan",
"Donald Trump",
"Ato Ahoi",
"Kwame Nkrumah"
],
"correctAnswer":"Kwame Nkrumah"
}
//to select correct answer
print $jsDecode['correctAnswer'];