I pulled some data from my database. Is working perfectly. But I want to remove the double quotes in the JSON. Here are my codes;
$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);
Is echoing a data like this;
{"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"}
But I want it like this:
{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"}
The output PHP gave is correct, if you need this output:
{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"}
in javascript, try using..
var json = JSON.parse(response);
in php try using
$json = json_decode($json);
Hope this helps.
you could try to encode then decode it like this
$encodeJson = json_encode($json);
print_r(json_decode($encodeJson, true));
if you would like each offset then you can print it like this and no quotes will be added:
$decodeJson = json_decode($encodeJson, true);
print $decodeJson['question'];
EDIT: below code is comment answer:
$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'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.