I'm trying echo the contents of an object in a JSON format. I'm quite unexperienced with PHP and I was wondering if there is a predefined function to do this (like json_encode()) or do you have to build the string yourself? When Googling "PHP object to JSON", I'm just finding garbage.
class Error {
private $name;
private $code;
private $msg;
public function __construct($ErrorName, $ErrorCode, $ErrorMSG){
$this->name = $ErrorName;
$this->code = $ErrorCode;
$this->msg = $ErrorMSG;
}
public function getCode(){
return $this->code;
}
public function getName(){
return $this->name;
}
public function getMsg(){
return $this->msg;
}
public function toJSON(){
$json = "";
return json_encode($json);
}
}
What I want toJSON to return:
{ name: "the content of $name var", code : 1001, msg : error while doing request}
To encode objects into a JSON formatted string in PHP, you can use the json_encode(value, options, depth) function. The first parameter specifies the PHP object to encode. You can control how the PHP object will be encoded into JSON by passing a combination of bitmasks in the second parameter.
To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
json_encode() is a native PHP function that allows you to convert PHP data into the JSON format. The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails).
PHP File explained:Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode() function.
You're just about there. Take a look at get_object_vars in combination with json_encode and you'll have everything you need. Doing:
json_encode(get_object_vars($error));
should return exactly what you're looking for.
The comments brought up get_object_vars respect for visibility, so consider doing something like the following in your class:
public function expose() {
return get_object_vars($this);
}
And then changing the previous suggestion to:
json_encode($error->expose());
That should take care of visibility issues.
An alternative solution in PHP 5.4+ is using the JsonSerializable interface.
class Error implements \JsonSerializable
{
private $name;
private $code;
private $msg;
public function __construct($errorName, $errorCode, $errorMSG)
{
$this->name = $errorName;
$this->code = $errorCode;
$this->msg = $errorMSG;
}
public function jsonSerialize()
{
return get_object_vars($this);
}
}
Then, you can convert your error object to JSON with json_encode
$error = new MyError("Page not found", 404, "Unfortunately, the page does not exist");
echo json_encode($error);
Check out the example here
More information about \JsonSerializable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With