php - 为什么 json_encode 添加反斜杠?

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()); 给我 dollowing 结果:

({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%");
  });

最佳答案

只需使用“JSON_UNESCAPED_SLASHES”选项(在 5.4 版之后添加)。

json_encode($array,JSON_UNESCAPED_SLASHES);

https://stackoverflow.com/questions/10314715/

相关文章:

c# - 创建 JObject 时出现参数异常

jquery - 如何使用jquery将项目添加到无序列表