1

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.

9
  • 3
    What's wrong with using json_encode? Commented Mar 19, 2014 at 13:50
  • 2
    All the properties are private? what are you going to do with the json? Commented Mar 19, 2014 at 13:52
  • You can try serialize / unserialize functions. Commented Mar 19, 2014 at 13:55
  • you could give your classes a toJson method where you create an array consisting of your private variables and the njson_encode and return it?. Commented Mar 19, 2014 at 13:58
  • json_encode($myObj) returns {} Commented Mar 19, 2014 at 14:00

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

...well, where do i have to put it?
into the class you want to echo json of it, use $this->encodeJSON() and you have to write it into every object that have private members you want to JSON it
Does it work if i create an Json class with this method and i extend each class from this new Json class?
And you are telling me to read about encapsulation? Your properties are private so if you put this method in a class and extend it won't be able to access the private properties.
@MajedDH would have been nice if you quoted the original answer you pasted yours from.
|
1

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

1 Comment

that work really well for my complexe object. This solution doesn't add crazy name in the json object!
0

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

4 Comments

Are you sure that is the best way? I have 25 classes, i would have to do it in each single class. I guess there must be a way to do it through reflection in one single class or something else.
Well, with PHP 5.4 came the JsonSerializable interface that might have helped but with an earlier version of PHP, it's way more 'complicated' to achieve this. I'd suggest you have a look at this answer : stackoverflow.com/a/4697671/831669 and this one : stackoverflow.com/a/14796817/831669
I've already seen an example like this, it'd mean that i have to write a proper method for every class...
@Emaborsa or upgrade your outdated PHP version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.