复杂的PHP对象到JSon字符串


Complex PHP Object to JSon String

我在PHP中有一个复杂的对象,需要解析它以构建一个Json字符串。我在这里和其他网站上找到了很多例子,但没有一个有效。更进一步的问题是,我的主机工作在PHP 5.2,我不能升级。

这是我的var_dump($myObj)的一个例子:

object(Park)[4]
private 'idObj' => string '60304' (length=5)
private 'name' => string 'AlphaSurf' (length=9)
private 'address' => 
object(Address)[6]
  private 'idObj' => string '40304' (length=5)
  private 'street' => string 'Champ de la Vigne' (length=17)
  private 'number' => string '7' (length=1)
  private 'zip' => string '1470' (length=4)
  private 'city' => string 'Estavayer-le-Lac' (length=16)
  private 'country' => 
    object(Country)[8]
      private 'idObj' => string '30039' (length=5)
      private 'name' => string 'Switzerland' (length=11)
      private 'flag' => string 'switzerland.gif' (length=15)
  private 'usState' => null
private 'contactInfo' => 
object(ContactInfo)[7]
  private 'idObj' => string '70304' (length=5)
  private 'phone' => string '' (length=0)
  private 'email' => string '' (length=0)
  private 'emailcode' => null
  private 'confirmed' => string '1' (length=1)
  private 'website' => string 'www.alphasurf.ch' (length=16)
  private 'mobile' => string '' (length=0)
  private 'fax' => string '' (length=0)
  private 'newsletter' => string '0' (length=1)
private 'owner' => 
object(User)[9]
  private 'idObj' => string '50001' (length=5)
  private 'username' => string 'emaborsa' (length=8)
  private 'password' => string '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8' (length=40)
  private 'type' => string 'ADMIN' (length=5)
  private 'state' => string 'ACTIVE' (length=6)
  private 'ip' => string '' (length=0)
  private 'time' => string '0' (length=1)
  private 'address' => null
  private 'contactInfo' => 
    object(ContactInfo)[11]
      private 'idObj' => string '1' (length=1)
      private 'phone' => null
      private 'email' => string 'info@emaborsa.com' (length=17)
      private 'emailcode' => null
      private 'confirmed' => string '1' (length=1)
      private 'website' => null
      private 'mobile' => null
      private 'fax' => null
      private 'newsletter' => string '1' (length=1)
private 'logo' => string 'Champ de la Vigne 71470' (length=23)
private 'xcoord' => string '46856912' (length=8)
private 'ycoord' => string '6846918' (length=7)
private 'state' => string 'HIDDEN' (length=6)
private 'detail' => 
object(ParkDetail)[10]
  private 'idObj' => string '1' (length=1)
  private 'descriptionIT' => string '' (length=0)
  private 'descriptionEN' => string '' (length=0)
  private 'descriptionDE' => string 'xcxcx' (length=5)
  private 'type' => string '' (length=0)
  private 'kickers' => string '0' (length=1)
  private 'boxes' => string '0' (length=1)
  private 'rails' => string '0' (length=1)
  private 'specials' => string '0' (length=1)
  private 'specialsDescriptionIT' => null
  private 'specialsDescriptionEN' => null
  private 'specialsDescriptionDE' => null
  private 'dimension' => string '0' (length=1)
private 'lastPayment' => null

所有属性都是私有的,但是有公共的getter和setter

试试这个

public function encodeJSON() 
{    
    foreach ($this as $key => $value) 
    { 
         if($value instanceOf(stdClass)){
             $json->$key = $value->encodeJSON();
         }else
             $json->$key = $value; 
    } 
    return json_encode($json);
}

我正试图将私有成员移动到一个可以由正常json_encode()写入的新对象,并且在第6行中,如果它不是基本类型

,我将递归地调用它。

我认为你需要暴露私有属性值,这是一个设计错误。

当然,在某些情况下应该这样做。正如PHP Object To JSON格式所指出的那样,这样做的一种方法是通过反射。

下面是一个简单的例子,使用php ReflectionClass来实现你想要的:

function getJson($object)
{
    $result = array();
    $refl = new ReflectionClass($object);
    foreach ($refl->getProperties() as $prop) {
        $prop->setAccessible(true);
        $result[$prop->name] = $prop->getValue($object);
    }
    return json_encode($result);
}

当你运行PHP <5.4,我建议在你的对象中创建一个toArray方法,返回一个包含所有属性的数组(如果它们是公共的,私有的或受保护的)。

例如:

public class Park {
    private $idObj;
    private $address;
    public function toArray() {
        $toArray = array(
            'idObj' => $this->idObj,
            'address' => $this->address->toArray() // Assuming address is an Address object
        );
    }
}

在你所有的类和子类中都这样做,然后你可以使用:

$park = new Park(/* your values to initialize the object */);
echo json_encode($park->toArray());