复杂的PHP对象到JSon字符串

时间:2014-03-19 13:48:38

标签: php object json

我在PHP中有一个复杂的对象,需要解析它以构建一个Json String。我在这里和其他网站找到了很多exmaples,但没有人工作。进一步的问题是我的托管在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。

3 个答案:

答案 0 :(得分:1)

试试这个

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行中,如果它不是一个主要类型,我会以递归方式调用它。 / p>

答案 1 :(得分:0)

正在运行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());

答案 2 :(得分:0)

我认为您需要公开私有属性值,从而导致设计错误。

但当然有些情况应该这样做。 正如PHP Object To JSON format所指出的,这样做的一种方法就是反思。

这是一个使用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);
}