对象怎么转成json
现在有一个对象
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 )
我要打包成json格式 (实际情况是要传到另个php端[webservice])
希望实现的具体流程:
php端1 -> 对象 -> json字符串 -> php端2 ->json字符串 -> 变回对象或者数组 (最好是数组)
用 json_decode 出来的总是 stdClass Object ( ) 不知道什么意思 求交流指点!
------最佳解决方案--------------------
因为你想还原成 user 对象,所以才让你用序列化函数
既然你不需要还原成php对象,那就用json好了
$p = new user;
$s = json_encode($p);
$o = json_decode($s); //还原成对象
$a = json_decode($s, true); //还原成数组
------其他解决方案--------------------
json_decode 出来的是OBJECT对象,没错。
如果你要decode成USER对象,可能就需要用到反射啦。。。
------其他解决方案--------------------
json 是与js交互用的数据格式,不会携带php才有的信息。所以不可能原样还原
你应该使用php的序列化函数
class T {<br /> public $var = 123;<br /> }<br /> $p = new T;<br /> $s = serialize($p);<br /> $x = unserialize($s);<br /> print_r($x);<br />T Object
(
[var] => 123
)
------其他解决方案--------------------
感谢版主的回复!对象序列化之后是这样的
O:4:"user":2:{s:13:"userUser_id";s:1:"1";s:13:"userAccount";s:3:"zzz";}
还不是json格式。莫非要截取字符串? 最好不要啦 - - 以后数据量大了 不知道有没隐患
直接序列化就传的话 也是字符串。但是做webservice要考虑到跨平台 比如传到java .net甚至其他的 所以最好还是能够传个json吧
简单点说,我的需求就是希望能把一个对象转成json 就最好了
user Object ( [User_id:private] => 1 [Account:private] => zzz )
成一个json格式
------其他解决方案--------------------
先谢谢您的关注!
现在如果将一个简单的对象直接json_encode() 输出的是一个空的对象 {}
还是达不到要求!
思路清晰了很多 继续研究!
------其他解决方案--------------------
找了解决方案了。
PHP能够将一个对象强转成数组 $user = json_encode((array)$user);
这样就可以打包成json了
对象 : user Object ( [User_id:private] => 1 [Account:private] => zzz )
转成数组后json_encode() : {"\u0000user\u0000User_id":1,"\u0000user\u0000Account":"zzz"}
接收到json用json_decode()转回数组 : Array ( [userUser_id] => 1 [userAccount] => zzz )
再次感谢xuzuning版主的帮助,谢谢!

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
