Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_decode to custom class

Tags:

json

php

Is it possible to decode a json string to an object other than stdClass?

like image 806
Tom Avatar asked Mar 22 '11 20:03

Tom


People also ask

What does json_decode return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

What is Json_encode and json_decode?

JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.

What is json_decode File_get_contents PHP input?

file_get_contents() function: This function in PHP is used to read a file into a string. json_decode() function: This function takes a JSON string and converts it into a PHP variable that may be an array or an object.

What is JSON encoding and decoding?

Source code: Lib/json/__init__.py. JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 4627) and by ECMA-404, is a lightweight data interchange format inspired by JavaScript object literal syntax (although it is not a strict subset of JavaScript 1 ).


2 Answers

Not automatically. But you can do it the old fashioned route.

$data = json_decode($json, true);  $class = new Whatever(); foreach ($data as $key => $value) $class->{$key} = $value; 

Or alternatively, you could make that more automatic:

class Whatever {     public function set($data) {         foreach ($data AS $key => $value) $this->{$key} = $value;     } }  $class = new Whatever(); $class->set($data); 

Edit: getting a little fancier:

class JSONObject {     public function __construct($json = false) {         if ($json) $this->set(json_decode($json, true));     }      public function set($data) {         foreach ($data AS $key => $value) {             if (is_array($value)) {                 $sub = new JSONObject;                 $sub->set($value);                 $value = $sub;             }             $this->{$key} = $value;         }     } }  // These next steps aren't necessary. I'm just prepping test data. $data = array(     "this" => "that",     "what" => "who",     "how" => "dy",     "multi" => array(         "more" => "stuff"     ) ); $jsonString = json_encode($data);  // Here's the sweetness. $class = new JSONObject($jsonString); print_r($class); 
like image 95
Michael McTiernan Avatar answered Oct 22 '22 15:10

Michael McTiernan


We built JsonMapper to map JSON objects onto our own model classes automatically. It works fine with nested/child objects.

It only relies on docblock type information for mapping, which most class properties have anyway:

<?php $mapper = new JsonMapper(); $contactObject = $mapper->map(     json_decode(file_get_contents('http://example.org/contact.json')),     new Contact() ); ?> 
like image 25
cweiske Avatar answered Oct 22 '22 14:10

cweiske