I have the following two sets of code that need to be decoded.
$test =
'{
"username":"sophia",
"event":{
"failure":"unreset",
"answers":{
"question1":"{\"answer\":\"{\\\"pass\\\":true,\\\"mark\\\":9,\\\"totalmark\\\":9,\\\"value\\\":\\\"get full attention|establish classroom rules|teach key words & concepts|use visual aids|demonstrate|check understanding|introduce point system|give handouts|play\\\"}\",\"state\":\"{\\\"result\\\":{\\\"pass\\\":true,\\\"mark\\\":9,\\\"totalmark\\\":9,\\\"value\\\":\\\"get full attention|establish classroom rules|teach key words & concepts|use visual aids|demonstrate|check understanding|introduce point system|give handouts|play\\\"}}\"}"
}
},
"event_source":"server"
}';
For the first one, I can't decode it at all although it is valid. It seems that the "question1" part is encoded twice and I don't know what's wrong with it.
$test =
'{
"username":"lon",
"event":{
"saved_response":"{\"parts\": [{\"text\": \"Passion for teaching means loving your job. Doing with all your heart. Teachers who are passionate can inspire pupils to love learning. Passionate teachers create an effective learning environment and increase learning potential of\\nstudents.\"}]}"
},
"event_source":"server"
}';
$jarray = json_decode($test, true);
$jevent = json_decode($jarray['event']['saved_response'], true);
For the second one, I can decode it once, but the output of var_dump($jevent)
is NULL.
Can anyone kindly explain to me why these two errors occur? I have checked How to deal with backslashes in json strings php and I'm really confused right now. Thanks.