Using json_encode for objects in PHP (regardless of scope)

I am trying to list object as json and would like to know if there is a way to make objects suitable for json_encode ? The code I have looks something like this:

 $related = $user->getRelatedUsers(); echo json_encode($related); 

Right now, I am just looping through an array of users and separately exporting them to arrays for json_encode to turn into useful json for me. I already tried to make objects iterable, but json_encode just skips them anyway.

edit: here var_dump ();

 php > var_dump($a); object(RedBean_OODBBean)#14 (2) { ["properties":"RedBean_OODBBean":private]=> array(11) { ["id"]=> string(5) "17972" ["pk_UniversalID"]=> string(5) "18830" ["UniversalIdentity"]=> string(1) "1" ["UniversalUserName"]=> string(9) "showforce" ["UniversalPassword"]=> string(32) "" ["UniversalDomain"]=> string(1) "0" ["UniversalCrunchBase"]=> string(1) "0" ["isApproved"]=> string(1) "0" ["accountHash"]=> string(32) "" ["CurrentEvent"]=> string(4) "1204" ["userType"]=> string(7) "company" } ["__info":"RedBean_OODBBean":private]=> array(4) { ["type"]=> string(4) "user" ["sys"]=> array(1) { ["idfield"]=> string(2) "id" } ["tainted"]=> bool(false) ["model"]=> object(Model_User)#16 (1) { ["bean":protected]=> *RECURSION* } } } 

and here is what json_encode gives me:

 php > echo json_encode($a); {} 

I ended up with this:

  function json_encode_objs($item){ if(!is_array($item) && !is_object($item)){ return json_encode($item); }else{ $pieces = array(); foreach($item as $k=>$v){ $pieces[] = "\"$k\":".json_encode_objs($v); } return '{'.implode(',',$pieces).'}'; } } 

It takes arrays full of these objects, or just single instances, and turns them into json - I use it instead of json_encode. I'm sure there are places where I could do it better, but I was hoping json_encode would be able to detect when iterating through an object based on its open interfaces.

+53
json scope php redbean
Jan 15 2018-11-11T00:
source share
8 answers

RedBeanPHP 2.0 has a bulk export function that turns the entire beans collection into arrays. This works with a JSON encoder.

 json_encode( R::exportAll( $beans ) ); 
+10
May 20 '11 at 7:32 a.m.
source share

All properties of your property are private. aka ... are not available outside their class.

Solution for PHP <5.4

If you want to serialize your private and protected objects, you need to implement the JSON encoding function inside your class, which uses json_encode() for the data structure that you create for this purpose.

 class Thing { ... public function to_json() { return json_encode(array( 'something' => $this->something, 'protected_something' => $this->get_protected_something(), 'private_something' => $this->get_private_something() )); } ... } 

Solution for PHP> = 5.4

Use the new JsonSerializable interface to provide your own json representation to be used by json_encode

 class Thing implements JsonSerializable { ... public function jsonSerialize() { return [ 'something' => $this->something, 'protected_something' => $this->get_protected_something(), 'private_something' => $this->get_private_something() ]; } ... } 

More detailed entry

+118
Jan 15 '11 at 2:50
source share

In PHP> = 5.4.0, a new interface for serializing objects in JSON has appeared: JsonSerializable

Just implement the interface in your object and define a JsonSerializable method that will be called when using json_encode .

So, the solution for PHP> = 5.4.0 should look something like this:

 class JsonObject implements JsonSerializable { // properties // function called when encoded with json_encode public function jsonSerialize() { return get_object_vars($this); } } 
+30
May 08 '12 at 15:02
source share

I have not seen this yet, but beans has a built-in method called getProperties() .

So, to use it:

 // What bean do we want to get? $type = 'book'; $id = 13; // Load the bean $post = R::load($type,$id); // Get the properties $props = $post->getProperties(); // Print the JSON-encoded value print json_encode($props); 

It is output:

 { "id": "13", "title": "Oliver Twist", "author": "Charles Dickens" } 

Now take a step forward. If we have an array of beans ...

 // An array of beans (just an example) $series = array($post,$post,$post); 

... then we could do the following:

  • Scroll the array using the foreach .

  • Replace each element (a bean) with an array of bean properties.

So this is ...

 foreach ($series as &$val) { $val = $val->getProperties(); } print json_encode($series); 

... outputs this:

 [ { "id": "13", "title": "Oliver Twist", "author": "Charles Dickens" }, { "id": "13", "title": "Oliver Twist", "author": "Charles Dickens" }, { "id": "13", "title": "Oliver Twist", "author": "Charles Dickens" } ] 

Hope this helps!

+2
Jun 30 '13 at 18:03
source share

The following code worked for me:

 public function jsonSerialize() { return get_object_vars($this); } 
+2
Mar 31 '14 at 12:55
source share

I usually include a small function in my objects that allows me to dump an array of either json or xml. Something like:

 public function exportObj($method = 'a') { if($method == 'j') { return json_encode(get_object_vars($this)); } else { return get_object_vars($this); } } 

anyway, get_object_vars() is probably useful to you.

0
Jan 15 '11 at 3:15
source share
 $products=R::findAll('products'); $string = rtrim(implode(',', $products), ','); echo $string; 
0
Sep 28 '15 at
source share

for an array of objects, I used something like this, following the custom method for php <5.4:

 $jsArray=array(); //transaction is an array of the class transaction //which implements the method to_json foreach($transactions as $tran) { $jsArray[]=$tran->to_json(); } echo json_encode($jsArray); 
-one
Mar 27 '14 at 16:35
source share



All Articles