Unable to decode JSON stripslashed String?
Does anyone know why this happens?
var_dump(json_decode(stri开发者_StackOverflow中文版pslashes(json_encode(array("O'Reiley"))))); // array(1) { [0]=> string(8) "O'Reiley" }
var_dump(json_decode(stripslashes(json_encode(array("O\'Reiley"))))); // NULL
Are '
used at all by the JSON functions?
I don't know for sure, but json_last_error() should :)
My guess, though, is that json_encode()
does something to the \'
that the stripslashes()
then breaks - e.g. add another "\" to escape the backslash.
Isn't fiddling with a json encoded string using striplslashes()
before it's decoded wrong anyway?
I didn't look at it too deeply, but it looks like your code is
Taking a PHP Array and turning it into a json string
Mucking with that string
Trying to decode the mucked string as json
Think of it like this
$json_string = json_encode(array("O\'Reiley");
$json_string = stripslashes($json_string);
//it's no longer json, its just some random non-conforming string
var_dump(json_decode($json_string))
You should try without stripslashes()
$result = json_encode(striptslashes(array("O\'Reiley")));
if(json_last_error() > 0){
$result = json_encode(array("O\'Reiley"));
}
精彩评论