为什么json_encode会添加反斜杠?

时间:2012-04-25 11:31:47

标签: php json

我已经使用json_encode很长一段时间了,到目前为止我没有遇到任何问题。 现在我正在使用上传脚本,我尝试在文件上传后返回一些JSON数据。

我有以下代码:

print_r($result); // <-- This is an associative array
echo json_encode($result); // <-- this returns valid JSON

这给了我以下结果:

// print_r result
Array
(
    [logo_url] => http://mysite.com/uploads/gallery/7f/3b/f65ab8165d_logo.jpeg
    [img_id] => 54
    [feedback] => Array
        (
            [message] => File uploaded
            [success] => 1
        )

)

// Echo result
{"logo_url":"http:\/\/mysite.com\/uploads\/gallery\/7f\/3b\/f65ab8165d_logo.jpeg","img_id":"54","feedback":{"message":"File uploaded","success":true}}

有谁可以告诉我为什么json_encode会添加斜杠?

更新

@Quentin说json_encode.parseJSON之间发生了一些事情,他是对的。

做一个alert(data.toSource());给了我一些结果:

({response:"{\"logo_url\":\"http:\\/\\/storelocator.com\\/wp-content\\/uploads\\/gallery\\/7f\\/3b\\/71b9520cfc91a90afbdbbfc9d2b2239b_logo.jpeg\",\"img_id\":\"62\",\"feedback\":{\"message\":\"File uploaded\",\"success\":true}}", status:200})

这不是有效的JSON。它还添加了status:200,我不知道它来自何处。

可能是Plupload bind对我的返回数据做了什么吗?

这是我的js脚本:

  uploader.bind('FileUploaded', function(up, file, data) {
    alert(data.toSource());
    $('#' + file.id + " b").html("100%");
  });

7 个答案:

答案 0 :(得分:60)

只需使用&#34; JSON_UNESCAPED_SLASHES&#34;选项(在5.4版之后添加)。

json_encode($array,JSON_UNESCAPED_SLASHES);

答案 1 :(得分:35)

  

有谁能告诉我为什么json_encode会添加斜杠?

当嵌入HTML脚本元素时,正斜杠字符可能会导致问题(当<前面会触发“脚本元素结束”的SGML规则时)。作为预防措施,它们会被逃脱。

  

因为当我尝试使用jQuery.parseJSON(响应);在我的js脚本中,它返回null。所以我猜它与斜线有关。

没有。在JSON中,"/""\/"是等效的。

您在问题中列出的JSON是有效的(您可以使用jsonlint进行测试)。您的问题可能与json_encodeparseJSON之间发生的事情有关。

答案 2 :(得分:27)

我刚刚在我的一些脚本中遇到过这个问题,它似乎正在发生,因为我正在将json_encode应用于另一个数组中的数组,该数组也是json编码的。如果在创建数据的脚本中有多个foreach循环,那么这很容易做到。始终在最后应用json_encode。

以下是发生的事情。如果你这样做:

$data[] = json_encode(['test' => 'one', 'test' => '2']);
$data[] = json_encode(['test' => 'two', 'test' => 'four']);
echo json_encode($data);

结果是:

["{\"test\":\"2\"}","{\"test\":\"four\"}"]

所以,你真正需要做的是:

$data[] = ['test' => 'one', 'test' => '2'];
$data[] = ['test' => 'two', 'test' => 'four'];
echo json_encode($data);

这将返回

[{"test":"2"},{"test":"four"}]

答案 3 :(得分:2)

这是因为JSON格式使用&#34;&#34;(引用),这些引号之间的任何内容都是有用的信息(键或数据)。

假设您的数据是:He said "This is how it is done". 然后实际数据应该看起来像"He said \"This is how it is done\"."

这可确保\"被视为"(Quotation mark),而不是JSON格式。这称为escape character

当人们试图编码已经存在JSON编码的数据时,通常会发生这种情况,这是我看到这种情况发生的常见方式。

试试这个

$arr = ['This is a sample','This is also a "sample"']; echo json_encode($arr);

输出:

["This is a sample","This is also a \"sample\""]

答案 4 :(得分:2)

确保您的 php 脚本具有正确的标题,否则它会添加斜杠 header('Content-Type: application/json');

答案 5 :(得分:0)

我有一个非常类似的问题,我准备好发布一个数组。在我的帖子中,我有这个:

json = JSON.stringfy(json);

这里的细节是我在laravel中使用刀片来构建一个三视图形式,所以我可以前后移动,我在每个后退和前进按钮验证之间以及当我在没有重新加载的情况下返回时我的json用反斜杠填充的页面。我在每次验证中都console.log(json),并意识到json被视为字符串而不是对象。

在结论中,我不应该同意json = JSON.stringfy(json),而是将其分配给另一个变量。

var aux = JSON.stringfy(json);

这样我将json保留为对象,而不是字符串。

答案 6 :(得分:-2)

json_encode将始终添加斜杠。

查看手册HERE

上的一些示例

这是因为如果有一些角色需要转义,那么就会产生问题。

要使用json,请解析你的json以确保删除斜杠

你是否删除了slashesthe json将被eval解析而没有任何问题。

<?php
$array = array('url'=>'http://mysite.com/uploads/gallery/7f/3b/f65ab8165d_logo.jpeg','id'=>54);
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
var x = jQuery.parseJSON('<?php echo json_encode($array);?>');
alert(x);
</script>

这是我的代码,我可以解析JSON。

检查您的代码在解析JSON时可能是您遗漏了某些内容