尝试通过PHP中的JSON发送URL时遇到问题

时间:2014-08-28 12:49:08

标签: php json

我正在使用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的网址?

2 个答案:

答案 0 :(得分:6)

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

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

json_encode($arr, JSON_UNESCAPED_SLASHES);

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

答案 1 :(得分:3)

在PHP&gt; 5.4你可以使用

json_encode($arr, JSON_UNESCAPED_SLASHES);

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