json_encode的斜线问题.为什么以及如何解决呢? [英] Slash issue with json_encode. Why and how to solve it?

查看:135
本文介绍了json_encode的斜线问题.为什么以及如何解决呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么用json_encode输出此日期("2011/7/11")会显示("2011\/7\/11")?

Why outputting this date ("2011/7/11") with json_encode displays ("2011\/7\/11")?

如何将"2011\/7\/11"转换为"2011/7/11"?

$data_go = '2011/7/11';
$ddmmyyy='([1-9][\d]{3})[- \/.]([0-1][\d])[- \/.]([0-3][\d])';
            if(preg_match("/$ddmmyyy$/", $data_go)) {
            $year = substr($data_go,0,4);
            $month = substr($data_go,5,2);
            $day = substr($data_go,8,2);
            $j2g = $this->convert_date->JalaliToGregorian($year, $month, $day);
             $ok = $j2g[0]."/".$j2g[1]."/".$j2g[2];
            }else {
              return FALSE;
            }
echo json_encode($ok); // output "2011\/7\/11"

推荐答案

在PHP 5.4中,您可以使用JSON_UNESCAPED_SLASHES:

In PHP 5.4, you can use JSON_UNESCAPED_SLASHES:

echo json_encode("2011/7/11", JSON_UNESCAPED_SLASHES);

否则,您必须进行一些琐碎的后处理

Otherwise, you have to do some trivial post-processing

str_replace('\\/', '/', json_encode("2011/7/11"));

请注意,\/是在JSON中表示/的有效方法.

Note that \/ is a valid way to represent / in JSON.

这篇关于json_encode的斜线问题.为什么以及如何解决呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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