Victor Lundgren
Victor Lundgren

Reputation: 115

PHP json_encode returning empty sctructure

I'm calling json_encode to sendo a objects array back to the user, and at other parts of my code it works properly, but at this one it return a empty structure string.

Here is the result when I call json_encode on the array before encoding:

array(3) {
  [0]=>
  object(MinaProxy)#5 (7) {
    ["carregado":"MinaProxy":private]=>
    bool(true)
    ["link":"MinaProxy":private]=>
    int(1)
    ["idMina":protected]=>
    int(1)
    ["qntOuro":protected]=>
    int(2000)
    ["x":protected]=>
    int(307)
    ["idPartida":protected]=>
    int(1)
    ["proximo":protected]=>
    int(1)
  }
  [1]=>
  object(MinaProxy)#6 (7) {
    ["carregado":"MinaProxy":private]=>
    bool(true)
    ["link":"MinaProxy":private]=>
    int(2)
    ["idMina":protected]=>
    int(2)
    ["qntOuro":protected]=>
    int(2000)
    ["x":protected]=>
    int(512)
    ["idPartida":protected]=>
    int(1)
    ["proximo":protected]=>
    int(2)
  }
  [2]=>
  object(MinaProxy)#7 (7) {
    ["carregado":"MinaProxy":private]=>
    bool(true)
    ["link":"MinaProxy":private]=>
    int(3)
    ["idMina":protected]=>
    int(3)
    ["qntOuro":protected]=>
    int(2000)
    ["x":protected]=>
    int(716)
    ["idPartida":protected]=>
    int(1)
    ["proximo":protected]=>
    NULL
  }
}

Here is the result after json_encode:

[{},{},{}]

As can be noticed, there is no special characters in the original array, so I do not think it is a encoding issue. Here is the code where I call json_encode:

elseif($dados['acao'] == 'recuperarMinas') {
    $i = 0;
    $array = array();
    while ($this->partida->minas->temProximo()) {
        $array[$i] = $this->partida->minas->getProximoAvancando();
        $array[$i]->getIdPartida();
        $i++;
    }
    $_SESSION['partida'] = $this->partida;
    $retornoJson = json_encode($array);
    return $retornoJson;
}

Upvotes: 0

Views: 976

Answers (3)

Andreas
Andreas

Reputation: 1150

It's because they are not public If you want json_encode private/protected variables you have to implement JSON serialization your self.

You could try considering jsonserializable (php 5.4+)with caution!!!!!!! Lets say that you want to serialize private/protected properties of your objects in your response to some http requests but in other cases you may not want these properties to be serialized or you may already run code(even third party) that assumes they are not

anyhow if you want to expose all your object's properties the code in your fuction could be something like this:

public function somethinghere() {
    return get_object_vars($this);
}

The full signature of the function was left out in purpose because you should decide what you want to do according to your context(I was having considerations before posting this but I hope this way criticism will be minimized ) This example may also be considered as bad because it exposes everything(you may consider creating an associative array that contain only the properties you want to expose )

working example

Upvotes: 0

user991
user991

Reputation: 1369

Problem is that your output has three Objects with private and protected attributes.

See this example

<?php

class sampleClass
{
    private $hello = 25;
}


$class = new sampleClass();

echo json_encode($class);

?>

Output will be {}

In your case you have array [] of three {} objects, and they are empty because all their attributes are private or protected.

But if you will change object attributes to be public, public $hello = 25; then output will be

{"hello":25}

Upvotes: 5

Halayem Anis
Halayem Anis

Reputation: 7785

You have protected and private attributes, so you can not use any functions, including json_encode() to access directly to your object's property.

Use JSONSerialize

Upvotes: 1

Related Questions