【问题标题】:Having trouble while trying to send URL via JSON in PHP尝试在 PHP 中通过 JSON 发送 URL 时遇到问题
【发布时间】:2014-10-22 08:11:41
【问题描述】:

我正在使用 PHP 开发 Android 应用程序的 Web 服务。我正在尝试以 JSON 格式发送带有其他数据的 URL。但是 URL 发送的数据在 URL 中显示了不需要的斜杠 (//)。

这是我正在使用的代码:

if(isset($_POST['category_id'])):

        $result=$db->sub_category($_POST['category_id']);
        if($result):
        $msg="Success";
        $arr = array();
        while($row=mysql_fetch_array($result)):
            $arr['response'][] = array('category' => $row['category'], 'image' => "http://intelmobizsolution.com/Iphone/upload/iphone/".$row['image'], 'msg'=>$msg,'status'=>true); 
        endwhile;
        $abc=json_encode($arr);
        echo json_encode($arr);
        endif;


endif;

但结果显示如下:

{"response":[{"category":"Administrative Support2","image":"http:\/\/intelmobizsolution.com\/Iphone\/upload\/iphone\/27792582102banner_02.jpg","msg":"Success","status":true}]} 

如何以我想要的格式发送带有 JSON 的 URL?

【问题讨论】:

    标签: php json


    【解决方案1】:

    最好的方法是接受斜线。他们绝对不会伤害。将它们转义为 JSON 字符串是完全可以接受的。

    如果你真的想摆脱它们,那么你可以使用:

    json_encode($arr, JSON_UNESCAPED_SLASHES);
    

    ...但是如果您的数据曾经包含字符串 </script>,并且您将 JSON 输出到 HTML 文档中的一些 JavaScript 中,那么您将破坏您的脚本。

    【讨论】:

    • 它给了我以下错误:json_encode() 只需要 1 个参数,2 但是,它与 str_replace('\\/', '/', json_encode("2011/7/ 11"));来自stackoverflow.com/questions/6743554/… 的帖子
    • 如果 json_encode 需要 1 个参数,那么您使用的 PHP 版本已过时。
    • hmm.. 是的,先生,您是绝对正确的,非常感谢您的解决方案.... :)
    【解决方案2】:

    在 PHP > 5.4 中你可以使用

    json_encode($arr, JSON_UNESCAPED_SLASHES);

    http://php.net/manual/en/function.json-encode.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 2017-07-20
      相关资源
      最近更新 更多