【问题标题】:Can't convert a simple object to JSONObject无法将简单对象转换为 JSONObject
【发布时间】:2013-10-05 02:05:37
【问题描述】:

我想从用户类转换一个对象,但是当我完成创建我的用户并尝试使用json_encode 来转换它时,它返回一个空的 JSONObject -> {} 这是我的班级用户:

    <?php

class User{

    private $id; 
    private $nom;
    private $prenom;
    private $idImage;
    private $identifiant;
    private $email;
    private $password;
    private $dateNaissance;
    private $dateInscription;
    private $pays;
    private $codePostal;
    private $telephone;

    public function __construct($id = null, $nom = "", $prenom = "", $idImage = null, $identifiant = "", $email = "",$password = "", $dateNaissance = "", $dateInscription = "", $pays = "", $codePostal = "", $telephone = "") {
        $this->setId($id);
        $this->setNom($nom);
        $this->setPrenom($prenom);
        $this->setIdImage($idImage);
        $this->setIdentifiant($identifiant);
        $this->setEmail($email);
        $this->setPassword($password);
        $this->setDateNaissance($dateNaissance);
        $this->setDateInscription($dateInscription);
        $this->setPays($pays);
        $this->setCodePostal($codePostal);
        $this->setTelephone($telephone);
    }
    public function setId($id){
        $this->id = $id; 
    }
    public function getNom() {
        return $this->nom;
    }

    public function setNom($nom,$notify = false) {
        $this->nom = $nom;
        if($notify) $this->notifyObservers ();
    }

    public function getPrenom() {
        return $this->prenom;
    }

    public function setPrenom($prenom,$notify = false) {
        $this->prenom = $prenom;
        if($notify) $this->notifyObservers ();
    }

    public function getIdImage() {
        return $this->idImage;
    }

    public function setIdImage($imageProfile,$notify = false) {
        $this->idImage = $imageProfile;
        if($notify) $this->notifyObservers ();
    }

    public function getIdentifiant() {
        return $this->identifiant;
    }

    public function setIdentifiant($identifiant,$notify = false) {
        $this->identifiant = $identifiant;
        if($notify) $this->notifyObservers ();
    }

    public function getEmail() {
        return $this->email;
    }

    public function setEmail($email,$notify = false) {
        $this->email = $email;
        if($notify) $this->notifyObservers ();
    }

    public function getPassword() {
        return $this->password;
    }

    public function setPassword($password,$notify = false) {
        $this->password = $password;
        if($notify) $this->notifyObservers ();
    }

    public function getDateNaissance() {
        return $this->dateNaissance;
    }

    public function setDateNaissance($dateNaissance,$notify = false) {
        $this->dateNaissance = $dateNaissance;
        if($notify) $this->notifyObservers ();
    }

    public function getDateInscription() {
        return $this->dateInscription;
    }

    public function setDateInscription($dateInscription,$notify = false) {
        $this->dateInscription = $dateInscription;
        if($notify) $this->notifyObservers ();
    }

    public function getPays() {
        return $this->pays;
    }

    public function setPays($pays,$notify = false) {
        $this->pays = $pays;
        if($notify) $this->notifyObservers ();
    }

    public function getCodePostal() {
        return $this->codePostal;
    }

    public function setCodePostal($codePostal,$notify = false) {
        $this->codePostal = $codePostal;
        if($notify) $this->notifyObservers ();
    }

    public function getTelephone() {
        return $this->telephone;
    }

    public function setTelephone($telephone,$notify = false) {
        $this->telephone = $telephone;
        if($notify) $this->notifyObservers ();
    }
    public function __toString() {
        return $this->nom . $this->password . $this->identifiant;
    }
}

?> 

这里有我的测试文件:

<?php
require_once './UserDAO.class.php';
require_once './User.class.php';
$userDAO = new UserDAO(); 
$user = new User(); 
$json= json_encode($user);
echo $user; 
echo $json; 
?>

另外,我验证了用户是否真的被创建了,而且确实如此,所以我现在真的迷路了。所以感谢您的关注!

【问题讨论】:

  • 你所有的类属性都是私有的,json_encode 看不到它们来编码它们
  • 嗨,谢谢你,成功了!你能发布你的答案,我可以验证它吗?

标签: php json object


【解决方案1】:

如果你想json_encode一个对象(并且你使用的是PHP 5.4+)你可以实现JsonSerializable

<?php
class User implements \JsonSerializable
{
    private $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function jsonSerialize()
    {
        return array(
            'name'  => $this->name,
        );
    }
}

$u = new User('here');

echo json_encode($u), PHP_EOL;

否则,您将无法使用公共属性。

【讨论】:

  • 这是一个很棒的解决方案,绝对是我会使用的解决方案!谢谢,我会在 4 分钟内接受您的答复!
猜你喜欢
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
相关资源
最近更新 更多