如何在不转义斜杠的情况下使用json_encode?

时间:2022-04-15 00:28:01

I'm encoding it like so..

我是这样编码的..

json_encode($array_list, JSON_UNESCAPED_SLASHES)

Ex: \n turns into \\n, \r\n turns into \\r\\n

例如:\ n变为\\ n,\ r \ n变成\\ r \\ n

But, it's still escaping the slashes! What's wrong and how to fix it? Thanks.

但是,它仍在逃避冲击!怎么了?怎么解决?谢谢。

2 个解决方案

#1


19  

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

工作示例http://codepad.viper-7.com/LvWMhq

#2


0  

If it is a concern when doing any MySQL queries then you can use it like this:

如果在进行任何MySQL查询时这是一个问题,那么您可以像这样使用它:

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.

在此之前无需转义$ array本身中的任何内容,只需让mysql_real_escape_string转义json_encoded字符串即可。

#1


19  

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

工作示例http://codepad.viper-7.com/LvWMhq

#2


0  

If it is a concern when doing any MySQL queries then you can use it like this:

如果在进行任何MySQL查询时这是一个问题,那么您可以像这样使用它:

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.

在此之前无需转义$ array本身中的任何内容,只需让mysql_real_escape_string转义json_encoded字符串即可。