I have tried lots of things and could not get through yet. I have a json string with some single quotes, double quotes and some other characters which are causing an error when I am trying to iterate the json by converting in into an array.
The quotes are escapes using addslashes and are going correctly in the database. But when I am converting the string to array using json_decode and stripslashes it says invalid argument passed to for loop.
Please suggest.
Thanks in advance.
Do this on the quotes:
$str = htmlentities($str, ENT_QUOTES);
And they will get-off your way.
As I remember, you have to first escape the quotes in php, and then escape the quotes and slashes when you print the json_encode. So, for "\"text\"" the output has to be \"\\"text\\"\".
Related
is it possible to strip double quotes from json encoded array?
Exaample:
{"96":{"term_id":96,"name":"AS Roma","slug":"as-roma","term_group":0,"term_taxonomy_id":100,"taxonomy":"klient","description":"HWDP","parent":0,"count":5,"object_id":1800,"filter":"raw"},"100":{"term_id":100,"name":"Atletico Madrid","slug":"atletico-madrid","term_group":0,"term_taxonomy_id":104,"taxonomy":"klient","description":""vamos amigos"","parent":0,"count":6,"object_id":1800,"filter":"raw"}}
This is an array of values from wordpress get_the_terms function. As you may see one of those values "description":""vamos amigos"" has doubled double quotes (the input text in description is inside quotes). Which gives me an error on ajax response. I've tried everything to strip this double quotes - nothing works for me?
I would appreciate any indications.
I'm encoding it like so..
json_encode($array_list, JSON_UNESCAPED_SLASHES)
Ex: \n turns into \\n, \r\n turns into \\r\\n
But, it's still escaping the slashes! What's wrong and how to fix it? Thanks.
I think it is because of single and double quotes, see the examples
$arr = array("\n\r");
echo json_encode($arr,JSON_UNESCAPED_SLASHES); // ["\n\r"]
$arr = array('\n\r');
echo json_encode($arr,JSON_UNESCAPED_SLASHES); //["\\n\\r"]
working example http://codepad.viper-7.com/LvWMhq
If it is a concern when doing any MySQL queries then you can use it like this:
mysql_real_escape_string(json_encode($array))
No need to escape anything in the $array itself before this point, just let mysql_real_escape_string escape the json_encoded string.
Hi I'm having some difficulty with escaping double quoutes from a string.
Here's my situation:
I get the the result set from the database then I apply utf8_encode to it because there's latin/accented characters and it return the string as it should be exept the double quotes in the begin and end of the string.
If in the DB I have: "Olá João" it returns: Olá João. The double quotes are ignored
$rs = mysql_fetch_array($query)
$text = utf8_encode($rs['l_reference']);
echo $text;
I tried using addslashes but without success.
I think its because "Olá João" is a multibyte string and you must use a different workaround for this. Try this one mb_addslashes
I"m using the function json_encode() to encode my data in php before sending it back to my page. However, single quotes and double quotes have been escaped and appear as /' and /" in javascript. Any idea how to get back those quotes without those slashes in javascript. I'm using jquery.
Use $.parseJSON() if your JSON is a string.
If after that, you still have slashes, you may have magic quotes on. Check with this...
var_dump(get_magic_quotes_gpc());
If you get TRUE, disable magic quotes.
I have the following JSON string, i try to decode with php json_decode but $postarray
is always NULL, can't work out why this is?
Running on Debian 5.0 Linux
php Client API version => 5.0.51a
Json version 1.2.1
$json = '{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}';
$postarray = json_decode($json);
print_r($postarray);
Thanks
The reason to escape double quotes (\") in a string, is if the string is double quoted.
Since you are escaping the double quotes, you should double (not single) quote your string, like this:
<?php
$json = "{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}";
$postarray = json_decode($json);
print_r($postarray);
?>
Live Example
If you do want to single quote your string, then don't escape the double quotes, or use stripslashes() like Andrei suggested.
You can read about the four ways to specify a string in PHP, and the differences among them, here.
Try this:
<?php
$json = stripslashes('{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}');
$postarray = json_decode($json);
print_r($postarray);
You should enclose it in double quotes.
The string will not be parsed because it is enclosed in single quotes, so the backslashes are literal. If you remove them, use stripslashes, or enclose the string in double quotes, you should have no problems.