Custom decode for JSON with PHP

1

I have a class:

class NodoArbolDHTML{
    var $id;
    var $name;
    var $data;
    var $children;


    public function __construct($id,$name,$resultado,$tipo){
       $this->id = $id;
       $this->name = $name;
       $this->data = new datos($resultado,$tipo);

    }

    public function anadirHijo($nodoHijo, $id){   
        //anadir un hijo
       if (!isset($this->children)){
          $this->children = array();
       }
       $this->children[$id] = $nodoHijo;
       $this->children = array_values($this->children);

    }
 } 

With which I can create an object of this type:

$root= NodoArbolDHTML Object ( 
  [id] => 38358 
  [name] => Anillo Critico 
  [data] => datos Object ( [color] => #008000 [type] => circle ) 
  [children] => Array ( 
    [0] => NodoArbolDHTML Object ( 
            [id] => 38347 
            [name] => Transito 
            [data] => datos Object ( [color] => #008000 [type] => circle ) 
            [children] => 
          )
    [1] => NodoArbolDHTML Object ( 
            [id] => 43052 
            [name] => Centros de Acceso 
            [data] => datos Object ( [color] => #008000 [type] => circle ) 
            [children] => 
          ) 
  ) 
) 

And I can access an object of the array:

print_r($root->children[0]); :

NodoArbolDHTML Object ( 
      [id] => 38347  
      [name] => Transito 
      [data] => datos Object ( 
                   [color] => #008000 
                   [type] => circle 
                ) 
      [children] => 
) 

Then I do a $arbol=json_encode($root) and get an object in JSON notation:

// $arbol
{
    "id": "38358",
    "name": "Anillo Critico",
    "data": {
        "$color": "#008000",
        "$type": "circle"
    },
    "children": [
        {
            "id": "38347",
            "name": "Transito",
            "data": {
                "$color": "#008000",
                "$type": "circle"
            },
            "children": null
        },
        {
            "id": "43052",
            "name": "Centros de Acceso",
            "data": {
                "$color": "#008000",
                "$type": "circle"
            },
            "children": null
        }
    ]
}

I need a function inside the class NodoArbolDHTML that does the reverse process, to which I give the JSON in JSON notation.

In this way, you could:

  • use the add functionHijo
  • Go through the array, looking for the object that interests me:

    print_r($root->children[0]);
    
    NodoArbolDHTML Object ( 
        [id] => 38347 
        [name] => Transito 
        [data] => datos Object ( [color] => #008000 [type] => circle ) 
        [children] => 
    ) 
    
asked by Mayte 18.08.2016 в 13:02
source

1 answer

1

It is not possible to perform with PHP's own functions, so you must use a custom solution. In the following link there is a solution to your problem in SO in English.

Specifically for your problem:

class NodoArbolDHTML {
    [...]

    public static function fromJSONObject($jsonObject) {
        $nodo = new NodoArbolDHTML($jsonObject->id, $jsonObject->name, $jsonObject->resultado, $jsonObject->tipo);

        if(count($jsonObject->children) > 0) {
          foreach($jsonObject->children as $child) {
            $nodo->anadirHijo(NodoArbolDHTML::fromJSONObject($child), $child->id);
          }
        }

        return $nodo;
    }
}

$arbol = NodoArbolDHTML::fromJSONObject(json_decode($json));
    
answered by 18.08.2016 / 13:27
source