Using json_encode on objects in PHP (regardless of scope)
In RedBeanPHP 2.0 there is a mass-export function which turns an entire collection of beans into arrays. This works with the JSON encoder..
json_encode( R::exportAll( $beans ) );
json_encode an array of objects with private properties
You can use the JsonSerializable
type like this:
class Car implements JsonSerializable
{
private $make, $model;
public function jsonSerialize() {
return array($this->make, $this->model);
}
}
var $car = new Car();
echo json_encode($car, JSON_PRETTY_PRINT);
php json_encode() output changes based on the array indices
Because array(0=>"zero",1=>"one")
is the same as array("zero", "one")
and the JSON encoder assumes the latter is a list (since that's how PHP does lists) and has no way to tell the former from the latter.
If you want to force json_encode
to treat all arrays as objects, pass JSON_FORCE_OBJECT
as the second argument:
json_encode(array(0=>"zero",1=>"one"), JSON_FORCE_OBJECT)
// {"0":"zero","1":"one"}
If you always want a json list instead, get rid of the keys (e.g. using array_values()
) before encoding.
Why json_encode returns empty brackets?
The array you show has all the properties as private. this mean that this value are not available outside their class's scope.
you can look at this SO for some suggestion
Using json_encode on objects in PHP (regardless of scope)
Create custom json_encode function
You should still use json_encode
to process conversion from PHP to JSON. You can transform your PHP object before calling json_encode
.
Instead of assigning array()
to "posDict"
key, try assigning new stdClass()
.
PHP json_encode multiple arrays into one object
Your last example is not valid JSON, curly braces always mean object with keys; instead you're treating it as an array. If you want an object, then add keys to the array in PHP like so:
$json=json_encode(array('a' => $table1, 'b' => $table2, 'c' => $table3));
This would then yield
{"a":{"table1":[{...}]},"b":{"table2":[{...}]},...}
Which seems to be what you want.
Do I need json.parse if I've used json_encode()?
Yes it is valid, because JSON syntax is a subset of JavaScript object literal syntax. Therefore if you inject JSON-encoded text into generated JS code in the way you've shown then it behaves as an object literal, just as if you'd written it there by hand.
JSON.parse
would be required if you were receiving a string containing JSON-encoded text into a JavaScript variable (e.g. as the result of an AJAX request) and needed to convert it from a string into a usable object.
weird behavior of json_encode function
Yes, it's because the array keys aren't consecutive anymore, so it's treated as an associative array, and PHP associative arrays become JavaScript objects, because JavaScript does not have associative arrays.
Use array_splice()
to cleanly remove elements from the array.
Complex PHP Object to JSon String
Try this
public function encodeJSON()
{
foreach ($this as $key => $value)
{
if($value instanceOf(stdClass)){
$json->$key = $value->encodeJSON();
}else
$json->$key = $value;
}
return json_encode($json);
}
i'm trying to move the private members to a new object that can be written by normal json_encode()
and in line 6 i'm calling it recursively foreach parameter if it not a primative type
Related Topics
How to Install/Enable the PHP Phar Extension
Call a C Program from PHP and Read Program Output
Composer Installing: the JSON Extension Is Missing
Executing a Shell Script in Background with PHP
Package 'Php5-Gd' Has No Installation Candidate
Can't Install PHP Package on Centos
Naming Convention Issues When Using Codeigniter in Windows and Linux
Why We Need Curlopt_Ssl_Verifypeer in Windows
How to Check If There Is a There Is a Wget Instance Running
How to Put PHP Inside JavaScript
PHP Get Url of Redirect from Source Url
How to Logout from a Session in PHP
Laravel: How to Change the Default Auth Password Field Name
PHP Create a Multidimensional Array from an Array with Relational Data