Home Backend Development PHP Tutorial 对象如何转成json

对象如何转成json

Jun 13, 2016 pm 01:02 PM
gt json nbsp private user

对象怎么转成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 />
Copy after login
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版主的帮助,谢谢!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1263
29
C# Tutorial
1236
24
10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

Combination of golang WebSocket and JSON: realizing data transmission and parsing Combination of golang WebSocket and JSON: realizing data transmission and parsing Dec 17, 2023 pm 03:06 PM

The combination of golangWebSocket and JSON: realizing data transmission and parsing In modern Web development, real-time data transmission is becoming more and more important. WebSocket is a protocol used to achieve two-way communication. Unlike the traditional HTTP request-response model, WebSocket allows the server to actively push data to the client. JSON (JavaScriptObjectNotation) is a lightweight format for data exchange that is concise and easy to read.

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

What is the difference between MySQL5.7 and MySQL8.0? What is the difference between MySQL5.7 and MySQL8.0? Feb 19, 2024 am 11:21 AM

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string Nov 18, 2023 pm 01:59 PM

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string. When writing programs in Golang, we often need to convert the structure into a JSON string. In this process, the json.MarshalIndent function can help us. Implement formatted output. Below we will explain in detail how to use this function and provide specific code examples. First, let's create a structure containing some data. The following is an indication

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

See all articles