json_encode为空,没有错误

时间:2018-04-18 07:26:01

标签: php json

我有一个对象。我需要转换为JSON进行存储,但是当我尝试将其编码为JSON时,它会返回一个空的JSON对象。当我尝试使用json_last_error时。

我使用的代码

echo $payload["sub"];
echo json_encode($user);
echo json_last_error_msg();

我得到的结果

"102573480781696194937{}No error".

用户类我尝试编码

<?php
    /**
     * Created by PhpStorm.
     * User: Student
     * Date: 13-4-2018
     * Time: 10:40
     */

    namespace php;
    class User
    {
        private $isAdmin = false;
        private $registeredFood = array();
        private $googleID;
        private $name;
        private $notes = array();
        private $email;

        /**
         * User constructor.
         * @param $googleID
         */
        public function __construct($googleID)
        {
            $this->googleID = $googleID;

        }


        /**
         * @return mixed
         */
        public function getGoogleID()
        {
            return $this->googleID;
        }

        /**
         * @return bool
         */
        public function isAdmin()
        {
            return $this->isAdmin;
        }

        /**
         * @param bool $isAdmin
         */
        public function setIsAdmin($isAdmin)
        {
            $this->isAdmin = $isAdmin;
        }

        /**
         * @return array
         */
        public function getRegisteredFood()
        {
            return $this->registeredFood;
        }

        /**
         * @param array $registeredFood
         */
        public function setRegisteredFood($registeredFood)
        {
            $this->registeredFood = $registeredFood;
        }

        /**
         * @return mixed
         */
        public function getName()
        {
            return $this->name;
        }

        /**
         * @param mixed $name
         */
        public function setName($name)
        {
            $this->name = $name;
        }

        /**
         * @return array
         */
        public function getNotes()
        {
            return $this->notes;
        }

        /**
         * @param array $notes
         */
        public function setNotes($notes)
        {
            $this->notes = $notes;
        }

        /**
         * @return mixed
         */
        public function getEmail()
        {
            return $this->email;
        }

        /**
         * @param mixed $email
         */
        public function setEmail($email)
        {
            $this->email = $email;
        }
    }
?>

我希望有人可以帮助我

2 个答案:

答案 0 :(得分:4)

这是因为您班级的属性是私有的。

仅包含私有属性的示例类...

uint8_t test[25] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,4,0,0,0, 0xED};
HAL_SPI_Transmit_DMA(&hspi2, test, sizeof(test));

不公开价值:

php > class Foo { private $bar = 42; }
php > $obj = new Foo();

但是具有公共属性的示例类......

php > echo json_encode($obj);
{}

做到这一点!

php > class Bar { public $foo = 42; }
php > $objBar = new Bar();

\ JsonSerializable

PHP提供了一个需要方法php > echo json_encode($objBar); {"foo":42} 的&teff; \ JsonSerializable。此方法由json_encode()自动调用。

jsonSerialize

我更喜欢这种解决方案,因为不公开公开属性

序列化和反序列化......

如果你需要序列化和反序列化php对象,你可以......

class JsonClass implements JsonSerialize {
    private $bar;
    public function __construct($bar) {
        $this->bar = $bar;
    }
    public function jsonSerialize() {
        return [
            'foo' => $this->bar,
        ];
    }
}

$ serialized变量包含php > class Classe { public $pub = "bar"; } php > $obj = new Classe(); php > $serialized = serialize($obj); php > $original = unserialize($serialized); php > var_dump($original); php shell code:1: class Classe#2 (1) { public $pub => string(3) "bar" } 。如您所见,它不是json,而是一种允许您使用unserialize函数重新创建原始对象的格式。

答案 1 :(得分:0)

你有几个选择。

选项1:制作您的课程属性public

sensorario提到的内容类似,更改属性的可见性,以便可以从类外部访问它,这是您调用json_encode的位置。

选项2:在类中引入方法/函数以返回编码的JSON对象

toJson()课程中设置User个功能。

当然,还有更多选择 - 例如延长User以便User不会被“污染”等等。

但是,是的,一般问题是你的private属性。

相关问题