Page 1 of 1

json_encode() custom object type

Posted: Tue May 18, 2010 5:34 pm
by victorsk
Hi everyone,

Would anybody know how to use json_encode() to encode and preserve object type? What I have is the following:

Code: Select all

$client = new Web_Services_Object;
$client->CallAPI();
$obj = json_encode($client);
After json_decode($obj) is done, I get stdClass Object for:

Code: Select all


$myObj =  json_decode($obj); //is stdClass 

But I need $myObj to be of Web_Services_Object type. Does anybody know how object type Web_Services_Object can be preserved during json_encode() ???

Thank you,
Victor.

Re: json_encode() custom object type

Posted: Tue May 18, 2010 5:46 pm
by Eran
JSON objects are basically associative arrays and don't have specific types. They are property containers, the same as stdClass, which is why it is decoded into such. You might want to create a method in your class to load back serialized JSON data

Re: json_encode() custom object type

Posted: Tue May 18, 2010 6:40 pm
by victorsk
Hi,

Thanks for the message. Could you kindly post an example of what you mean? I am not much knowledgable with PHP :(

Thanks,
Victor.