用json_encode()获取一个空字符串

时间:2011-06-17 16:30:08

标签: php json

参见附件代码。由于某种原因,json_encode()返回一个空字符串 我用$jv = Video::ConvertToJson($video);来称呼它 使用断点我确认$video不为空等。
(使用PHP 5.3)
任何想法?
THX

class Video
{
    private $ID;
    private $Title;
    private $ViewCount;
    private $Description;
    private $IsEmbeddable;
    private $IsPrivate;

    function __construct($id = 0, $title = '', $viewcount=0, $description='', $isembeddable=1, $isprivate=0){
        $this->ID = $id;
        $this->Title = $title;
        $this->ViewCount = $viewcount;
        $this->Description = $description;
        $this->IsEmbeddable = $isembeddable;
        $this->IsPrivate = $isprivate;
    }
    /**
     *
     * Converts a Tfyoutubevideo into a json object
     * @param models\TfYoutubevideos $tfv
     */
     public static function ConvertToJson(models\TfYoutubevideos $tfv){
        $v = new Video();
        $v->ID = $tfv->getId();
        $v->Title = $tfv->getVideotitle();
        $v->ViewCount = $tfv->getVideoviewcount();
        $v->Description = $tfv->getVideoDescription();
        $v->IsEmbeddable = $tfv->getVideoIsEmbeddable();
        $v->IsPrivate = $tfv->getVideoIsPrivate();
        $vj = json_encode($v);
        return $vj;

     }
}

1 个答案:

答案 0 :(得分:5)

json_encode不会序列化私有(或受保护的)成员变量。将对象的状态复制到临时数组中或将成员变量声明为public以缓解该状态。