php 返回数据 json 的是 utf-8 ,接口需要返回中文 要求是 GBK, json_encode 只支持 utf-8

  public function toJson($content){
        if(is_string($content) ) {

            return urlencode($content);

        }elseif(is_array($content)){

            foreach ( $content as $key => $val ) {

                if($key == 'Status') {
                    $value = $val;
                }else {
                    $value = $this->toJson($val);
                }
                $content[$key] =$value;

            }

            return urldecode(json_encode($content));

        }elseif(is_object($content)) {

            $vars = get_object_vars($content);

            foreach($vars as $key=>$val) {

                $content[$key] =  $this->toJson($val);

            }
            return urldecode(json_encode($content));

        } else{

            return urldecode(json_encode($content));
        }

    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1

直接json_encode就可以了,因为encode会直接把中文转码成字符发送出去了,也就不存在需要考虑太多的格式

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
77
粉丝
8
喜欢
43
收藏
49
排名:84
访问:10.4 万
私信
所有博文
社区赞助商