处理JSON字符串中的转义斜杠 [英] Handling escaped slashes in a JSON string

查看:4249
本文介绍了处理JSON字符串中的转义斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取数据库的json,代码如下所示

I'm trying to get the json of the database and the code goes like this

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

当我转换为json对象时,它显示如下.

While I convert to a json object and it shows like this.

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

我刚刚应用了\来打印特殊字符/,但是它不起作用.我做了很多事情来打印特殊字符.将特殊字符转换为JSON是否有问题?

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?

推荐答案

正如其他人所提到的那样,在PHP或Javascript中,正斜杠不是字符串内的特殊字符(并且JSON是从Javascript派生的,遵循与字符串内插相同的规则).但是,如果您正在阅读JSON,则可以认为它是可以原谅的(尽管您应该总是 RTM ;-)).

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). However if you were reading some JSON, you could be forgiven for thinking that it is (although you should always RTM ;-) ).

之所以认为您需要转义斜线是因为PHP和Javascript插值多余的正斜线的方式之间存在细微的差异.考虑以下在PHP和Javascript中均有效的字符串声明:

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. Consider the following string declaration, valid in both PHP and Javascript:

"AC\/DC"

在PHP中,多余的反斜杠被视为文字,因此:

In PHP, the extra backslash is treated as a literal, so:

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

在Javascript中,多余的反斜杠被删除了,所以:

In Javascript, the extra backslash is dropped, so:

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

JSON强制转义正斜杠,但是 json_encode() 将解决此转义问题你.您不需要自己在字符串中添加反斜杠.而且由于这些其他反斜杠的插值方式不同,您不能简单地将JSON字符串拖放到PHP源中-因为它将被解释为不同的值.

JSON mandates the escaping of forward slashes, but json_encode() will take care of this escaping for you. 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.

自PHP 5.4.0起,您可以提供在PHP中将JSON_UNESCAPED_SLASHES标志标记为json_encode()以防止其添加反斜杠.但是,这是不必要的,并且可能导致严格的JSON解析器拒绝数据.

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. However this is unnecessary and may cause a strict JSON parser to reject the data.

总而言之,在PHP中声明字符串的正确方法是:

So to sum up, the correct way to declare your string in PHP is:

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

作为旁注,您还可能在字符串的开头包含http://并通过$row['file_name'] > urlencode() ,因为数据似乎是一个URL:

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:

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

这篇关于处理JSON字符串中的转义斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆