How does the object turn into JSON
Now there is an object
User Object ([user_id:private] = 1 [account:private] = ABC [password:private] = ABC [user_name:private] + = Zhangsan [Creat_time:private] = 2012-11-12 14:56:32)
I'm going to package it in JSON format (it's actually going to another PHP side [WebService])
The specific process you want to implement:
PHP 1, Object------JSON string----back to an object or an array (preferably an array), as a 2->json string
Use Json_decode out of Always stdClass Object () do not know what the meaning of the exchange guidance!
------Optimal Solution--------------------
Because you want to revert to the user object, you are allowed to use the serialization function
Since you don't need to revert to a PHP object, use JSON as well.
$p = new User;
$s = Json_encode ($p);
$o = Json_decode ($s); Revert to Object
$a = Json_decode ($s, true); Restore an array
------Other Solutions--------------------
Json_decode out is object, yes.
If you want to decode into a user object, you may need to use reflection ...
------Other Solutions--------------------
JSON is a data format that interacts with JS and does not carry the information that PHP has. So it can't be restored.
You should use the PHP serialization function
Class T {
Public $var = 123;
}
$p = new T;
$s = serialize ($p);
$x = Unserialize ($s);
Print_r ($x);
T Object
(
[var] = 123
)
------Other Solutions--------------------
Thanks to the Moderator's reply! This is what happens after the object is serialized.
O:4: "User": 2:{s:13: "useruser_id", s:1: "1"; s:13: "UserAccount"; s:3: "zzz";}
Not yet in JSON format. Do you want to intercept the string? It's best not to--after the data is large, I don't know if there's any danger.
Direct serialization is also a string of words. But doing webservice should take into account the cross-platform, such as to Java. NET, or whatever, so it's better to pass a JSON.
To put it simply, my need is to be able to turn an object into JSON, which is best.
User Object ([user_id:private] = 1 [account:private] = zzz)
into a JSON format
------Other Solutions--------------------
Thank you for your attention first!
Now if a simple object is directly json_encode () The output is an empty object {}
Still can't reach the request!
Thinking clear a lot of continue to study!
------Other Solutions--------------------
Find a solution.
PHP can convert an object to a group $user = Json_encode ((array) $user);
So you can package it up as JSON.
Object: User Object ([user_id:private] = 1 [account:private] = zzz)
After Json_encode (): {"\u0000user\u0000user_id": 1, "\u0000user\u0000account": "ZZZ"}
Received JSON with Json_decode () back to array: Array ([useruser_id] = 1 [useraccount] = zzz)
Thanks again for Xuzuning Moderator's help, thank you!