2014-03-19 194 views
1

我有一個複雜的對象在PHP,需要解析它來建立一個JSON字符串。我發現了很多我在這裏和其他網站找到的exmaples,但沒有人工作。進一步的問題是,我的託管在PHP 5.2上工作,我無法升級。複雜的PHP對象JSon字符串

這裏是我的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 '[email protected]' (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

這有什麼錯用'json_encode'? –

+2

所有屬性都是私人的?你打算怎麼處理json? – Steve

+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行我打電話是遞歸的foreach參數,如果它不是一個靈長類動物被寫入新對象鍵入

+0

......好吧,我必須把它放在哪裏? – Emaborsa

+0

到你想要回聲json的類中,使用'$ this-> encodeJSON()',並且你必須將它寫入到每個有私人成員的對象中去JSON它 –

+0

如果我創建一個Json類用這種方法,我從這個新的Json類擴展每個類? – Emaborsa

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()); 
+0

你確定這是最好的方法嗎?我有25班,我將不得不在每個班上完成。我猜想必須有一種方法可以通過在一個班級或其他課程中進行反思來完成。 – Emaborsa

+0

那麼,在PHP 5.4中出現了JsonSerializable接口,它可能有助於PHP的早期版本,但實現這一點更加「複雜」。我建議你看看這個答案:http://stackoverflow.com/a/4697671/831669和這一個:http://stackoverflow.com/a/14796817/831669 –

+0

我已經看到一個例子像這樣,這意味着我必須爲每個班級編寫適當的方法... – Emaborsa

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); 
}