Emaborsa
Emaborsa

Reputation: 2830

Complex PHP Object to JSon String

I have a complex object in PHP and need to parse it to build a Json String. I found many exmaples i've found here and other sites, but no one worked. The further problem is that my hosting works on PHP 5.2 and i cannot upgrade.

Here is an example of my 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

All properties are private but there are public getters and setters.

Upvotes: 1

Views: 1057

Answers (3)

YRM
YRM

Reputation: 301

I think you made a design mistake by needing to expose private property values.

But of course there are scenarios where this should be done. As pointed out by PHP Object To JSON format one way of doing this would be trough reflection.

Here is a simple example using php ReflectionClass to achieve what you want:

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

Upvotes: 1

Majed DH
Majed DH

Reputation: 1301

Try this

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

i'm trying to move the private members to a new object that can be written by normal json_encode() and in line 6 i'm calling it recursively foreach parameter if it not a primative type

Upvotes: 2

BMN
BMN

Reputation: 8508

As you're running PHP < 5.4, I'd recommend creating a toArray method in your objects, returning an array containing all the properties (without bothering if they are public, private or protected).

For example :

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

Do this in all your classes and sub-classes and then you can use :

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

Upvotes: 0

Related Questions