I am developing a web application in PHP,
I need to transfer many objects from server as JSON string, is there any library existing for PHP to convert object to JSON and JSON String to Objec, like Gson library for Java.
Script47
13.8k4 gold badges43 silver badges61 bronze badges
asked Mar 25, 2012 at 6:58
This should do the trick!
// convert object => json
$json = json_encode[$myObject];
// convert json => object
$obj = json_decode[$json];
Here's an example
$foo = new StdClass[];
$foo->hello = "world";
$foo->bar = "baz";
$json = json_encode[$foo];
echo $json;
//=> {"hello":"world","bar":"baz"}
print_r[json_decode[$json]];
// stdClass Object
// [
// [hello] => world
// [bar] => baz
// ]
If you want the output as an Array instead of an Object, pass true
to json_decode
print_r[json_decode[$json, true]];
// Array
// [
// [hello] => world
// [bar] => baz
// ]
More about json_encode[]
See also: json_decode[]
answered Mar 25, 2012 at 7:00
mačekmaček
74k36 gold badges164 silver badges196 bronze badges
2
json_decode[$json, true];
// the second param being true will return associative array. This one is easy.
answered Mar 25, 2012 at 10:31
Kishor KundanKishor Kundan
3,0751 gold badge22 silver badges30 bronze badges
PHP8-Code:
class foo{
function __construct[
public $bar,
protected $bat,
private $baz,
]{}
function getBar[]{return $this->bar;}
function getBat[]{return $this->bat;}
function getBaz[]{return $this->baz;}
}
//Create Object
$foo = new foo[bar:"bar", bat:"bat", baz:"baz"];
//Object => JSON
$fooJSON = json_encode[serialize[$foo]];
print_r[$fooJSON];
// "O:3:\"foo\":3:{s:3:\"bar\";s:3:\"bar\";s:6:\"\u0000*\u0000bat\";s:3:\"bat\";s:8:\"\u0000foo\u0000baz\";s:3:\"baz\";}"
// Important. In order to be able to unserialize[] an object, the class of that object needs to be defined.
# More information here: //www.php.net/manual/en/language.oop5.serialization.php
//JSON => Object
$fooObject = unserialize[json_decode[$fooJSON]];
print_r[$fooObject];
//[
# [bar] => bar
# [bat:protected] => bat
# [baz:foo:private] => baz
# ]
//To use some functions or Properties of $fooObject
echo $fooObject->bar;
// bar
echo $fooObject->getBat[];
// bat
echo $fooObject->getBaz[];
// baz
answered Mar 10, 2021 at 11:02
1
I made a method to solve this. My approach is:
1 - Create a abstract class that have a method to convert Objects to Array [including private attr] using Regex. 2 - Convert the returned array to json.
I use this Abstract class as parent of all my domain classes
Class code:
namespace Project\core;
abstract class AbstractEntity {
public function getAvoidedFields[] {
return array [];
}
public function toArray[] {
$temp = [ array ] $this;
$array = array [];
foreach [ $temp as $k => $v ] {
$k = preg_match [ '/^\x00[?:.*?]\x00[.+]/', $k, $matches ] ? $matches [1] : $k;
if [in_array [ $k, $this->getAvoidedFields [] ]] {
$array [$k] = "";
} else {
// if it is an object recursive call
if [is_object [ $v ] && $v instanceof AbstractEntity] {
$array [$k] = $v->toArray[];
}
// if its an array pass por each item
if [is_array [ $v ]] {
foreach [ $v as $key => $value ] {
if [is_object [ $value ] && $value instanceof AbstractEntity] {
$arrayReturn [$key] = $value->toArray[];
} else {
$arrayReturn [$key] = $value;
}
}
$array [$k] = $arrayReturn;
}
// if it is not a array and a object return it
if [! is_object [ $v ] && !is_array [ $v ]] {
$array [$k] = $v;
}
}
}
return $array;
}
}
answered Feb 22, 2016 at 13:36