简体   繁体   English

处理JSON字符串中的转义斜杠

[英]Handling escaped slashes in a JSON string

I'm trying to get the json of the database and the code goes like this 我正在尝试获取数据库的json并且代码如下所示

   $path["file_name"] = "www.119.com\/assets\/demo\/large\/".$row["file_name"];

While I convert to a json object and it shows like this. 当我转换为json对象时,它显示如下。

www.119.com\\\/assets\\\/demo\\\/large\\\/demo1.png

I just applied \\ to print special character / , but it's not working. 我只是应用\\来打印特殊字符/ ,但是它不起作用。 I applied many things to print the special character. 我做了很多事情来打印特殊字符。 Is it a problem in converting special character to JSON? 将特殊字符转换为JSON是否有问题?

As has been mentioned by others, a forward slash is not a special character inside a string, in PHP or in Javascript (and since JSON is derived from Javascript it follows the same rules for string interpolation). 正如其他人提到的那样,在PHP或Javascript中,正斜杠不是字符串中的特殊字符(并且JSON是从Javascript派生的,因此它遵循相同的字符串插值规则)。 However if you were reading some JSON, you could be forgiven for thinking that it is (although you should always RTM ;-) ). 但是,如果您正在阅读某些JSON,则可以认为它是可以原谅的(尽管您应该始终使用 RTM ;-))。

The reason you think you need to escape the slash is due to a subtle difference in the way PHP and Javascript interpolate superfluous forward slashes. 之所以认为您需要转义斜线是因为PHP和Javascript插值多余的正斜线的方式存在细微的差异。 Consider the following string declaration, valid in both PHP and Javascript: 考虑以下在PHP和Javascript中均有效的字符串声明:

"AC\/DC"

In PHP, the extra backslash is treated as a literal, so: 在PHP中,多余的反斜杠被视为文字,因此:

echo "AC\/DC"; // outputs AC\/DC

In Javascript, the extra backslash is dropped, so: 在Javascript中,多余的反斜杠被删除,因此:

console.log("AC\/DC"); // logs AC/DC

JSON mandates the escaping of forward slashes, but json_encode() will take care of this escaping for you. JSON强制转义正斜杠,但是json_encode()将为您处理此转义。 You do not need to add the backslashes to the string yourself. 您不需要自己在字符串中添加反斜杠。 And because of the difference in the way these additional backslashes are interpolated, you cannot simply take a JSON string and drop it into your PHP source - because it will will be interpretted as a different value. 而且由于这些其他反斜杠的插值方式不同,您不能简单地将JSON字符串拖放到PHP源中-因为它将被解释为不同的值。

Since PHP 5.4.0 you can supply the JSON_UNESCAPED_SLASHES flag to json_encode() in PHP to prevent it from adding the backslashes. 从PHP 5.4.0开始,您可以在PHP中为json_encode()提供JSON_UNESCAPED_SLASHES标志,以防止其添加反斜杠。 However this is unnecessary and may cause a strict JSON parser to reject the data. 但是,这是不必要的,并且可能导致严格的JSON解析器拒绝数据。

So to sum up, the correct way to declare your string in PHP is: 综上所述,在PHP中声明字符串的正确方法是:

$path["file_name"] = "www.119.com/assets/demo/large/".$row["file_name"];

As a side note, you probably also what to include http:// at the beginning of the string and pass $row['file_name'] through urlencode() as well, since the data appears to be a URL: 附带说明一下,您还可能在字符串的开头添加http://并通过urlencode()传递$row['file_name'] ,因为数据似乎是一个URL:

$path["file_name"] = "http://www.119.com/assets/demo/large/".urlencode($row["file_name"]);

There should be no need to escape the slash as its not considered a special character. 斜线不被视为特殊字符,因此无需逃脱。

You might need to replace / with // as some systems strip a single slash when its being parsed/displayed, windows comes to mind. 您可能需要将//替换为//,因为某些系统在解析/显示斜杠时会删除单个斜杠,因此您会想到Windows。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM