JSON格式化与serialize序列化,jsonserialize
JSON格式化与serialize序列化,jsonserialize
一、JSON格式化
1. JSON是什么
JSON是一种数据的存储格式,用来沟通客户端Javascript和服务端PHP的交互。我们把用PHP生成JSON后的字符串传给前台Javascript,Javascirpt就可以很容易的将其反JSON然后应用。
2. 如何使用JSON
PHP操作JSON可以使用json_encode()和json_decode()两个函数——一个编码,一个解码。json_encode()可以将数组转换成json格式的文本数据,方便存储和读取,而json_decode()可以直接将json数据转换成数组,方便调用。
<code class="hljs vbscript-html"><span class="xml"><span class="php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-variable">$arr</span> = <span class="hljs-keyword">array</span>( <span class="hljs-string">'name'</span> =<span class="hljs-string">'刘璐'</span>, <span class="hljs-string">'nick'</span> =<span class="hljs-string">'璐小璐'</span>, <span class="hljs-string">'age'</span> =<span class="hljs-string">'26'</span>,`` <span class="hljs-string">'contact'</span> =<span class="hljs-keyword">array</span>( <span class="hljs-string">'phone'</span> =<span class="hljs-string">'13718136109'</span>, <span class="hljs-string">'address'</span> =<span class="hljs-string">'Beijing ifdoo'</span> ) ); <span class="hljs-variable">$str</span> = json_encode(<span class="hljs-variable">$arr</span>); <span class="hljs-keyword">echo</span> <span class="hljs-string">"getProfile($str)"</span>; <span class="hljs-preprocessor">?></span></span> </span></code>
结果为:
{"name":"\u5218\u7490","nick":"\u7490\u5c0f\u7490","age":"26","contact":{"phone":"13718136109","address":"\u5317\u4eac \u5f97\u8c46"}}
3. JSON格式的数据与WEB前端JS完成异步交互过程
PHP使用json_encode()将数组转换成json格式的数据后,此json字符串相当于JavaScript中的数组,赋给一个变量后,就可以对这个数组进行操作了。
<code class="hljs haskell"><script <span class="hljs-typedef"><span class="hljs-keyword">type</span>="text/javascript></span> var arr = {<span class="hljs-string">"name"</span>:<span class="hljs-string">"\u5218\u7490"</span>,<span class="hljs-string">"nick"</span>:<span class="hljs-string">"\u7490\u5c0f\u7490"</span>,<span class="hljs-string">"age"</span>:<span class="hljs-string">"26"</span>,<span class="hljs-string">"contact"</span>:{<span class="hljs-string">"phone"</span>:<span class="hljs-string">"13718136109"</span>,<span class="hljs-string">"address"</span>:<span class="hljs-string">"\u5317\u4eac \u5f97\u8c46"</span>}}; alert(arr.name); </script> </code>
4. 实例
index.html
<code class="hljs vbscript-html"><span class="xml"><span class="hljs-tag"><<span class="hljs-title">html</span>></span> <span class="hljs-tag"><<span class="hljs-title">head</span>></span> <span class="hljs-tag"><<span class="hljs-title">title</span>></span>json demo<span class="hljs-tag"></<span class="hljs-title">title</span>></span> <span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/javascript"</span>></span><span class="javascript"> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getProfile</span><span class="hljs-params">(str)</span> </span>{ <span class="hljs-keyword">var</span> arr = str; <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'nick'</span>).innerHTML = arr.nick; } </span><span class="hljs-tag"></<span class="hljs-title">script</span>></span> <span class="hljs-tag"></<span class="hljs-title">head</span>></span> <span class="hljs-tag"><<span class="hljs-title">body</span>></span> <span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"nick"</span>></span><span class="hljs-tag"></<span class="hljs-title">div</span>></span> <span class="hljs-tag"></<span class="hljs-title">body</span>></span> <span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/javascript"</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"./profile.php"</span>></span><span class="hljs-tag"></<span class="hljs-title">script</span>></span> <span class="hljs-tag"></<span class="hljs-title">html</span>></span> </span></code>
profile.php
<code class="hljs vbscript-html"><span class="xml"><span class="php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-variable">$arr</span> = <span class="hljs-keyword">array</span>( <span class="hljs-string">'name'</span> => <span class="hljs-string">'刘璐'</span>, <span class="hljs-string">'nick'</span> => <span class="hljs-string">'璐小璐'</span>, <span class="hljs-string">'age'</span> => <span class="hljs-string">'26'</span>, <span class="hljs-string">'contact'</span> => <span class="hljs-keyword">array</span>( <span class="hljs-string">'phone'</span> => <span class="hljs-string">'13718136109'</span>, <span class="hljs-string">'address'</span> => <span class="hljs-string">'Beijing ifdoo'</span> ) ); <span class="hljs-variable">$str</span> = json_encode(<span class="hljs-variable">$arr</span>); <span class="hljs-keyword">echo</span> <span class="hljs-string">"getProfile($str)"</span>; <span class="hljs-preprocessor">?></span></span> </span></code>
-
html页面调用PHP文件
<script language="text/javascript" src="/xx/a.php"></script>
a.php中的echo输出的是javascript代码。
-
php页面调用js文件
a.php中的echo js里的方法即可。
二、serialize序列化
1. serialize 是什么
serialize是将变量序列化,返回一个具有变量类型和结构的字符串表达式。
2. 如何使用serialize
使用PHP的serialize和unserialize将数组进行序列化和反序列化。
<code class="hljs vbscript-html"><span class="xml"><span class="php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-variable">$arr</span> = <span class="hljs-keyword">array</span>( <span class="hljs-string">"u1"</span> => <span class="hljs-keyword">array</span>( <span class="hljs-string">"gameName"</span> => <span class="hljs-string">"德乙"</span>, <span class="hljs-string">"homeName"</span> => <span class="hljs-string">"比勒费尔德"</span>, <span class="hljs-string">"guestName"</span> => <span class="hljs-string">"不伦瑞克"</span>, <span class="hljs-string">"endTime"</span> => <span class="hljs-string">"2015-08-21"</span> ), <span class="hljs-string">"u2"</span> => <span class="hljs-keyword">array</span>( <span class="hljs-string">"gameName"</span> => <span class="hljs-string">"英超"</span>, <span class="hljs-string">"homeName"</span> => <span class="hljs-string">"水晶宫"</span>, <span class="hljs-string">"guestName"</span> => <span class="hljs-string">"阿斯顿维拉"</span>, <span class="hljs-string">"endTime"</span> => <span class="hljs-string">"2015-08-22"</span> ) ); <span class="hljs-keyword">echo</span> serialize(<span class="hljs-variable">$arr</span>); <span class="hljs-preprocessor">?></span></span> </span></code>
结果为:
a:2:{s:2:"u1";a:4:{s:8:"gameName";s:6:"德乙";s:8:"homeName";s:15:"比勒费尔德";s:9:"guestName";s:12:"不伦瑞克";s:7:"endTime";s:10:"2015-08-21";}s:2:"u2";a:4:{s:8:"gameName";s:6:"英超";s:8:"homeName";s:9:"水晶宫";s:9:"guestName";s:15:"阿斯顿维拉";s:7:"endTime";s:10:"2015-08-22";}}
其中:
a:2说明这是个有两个元素的数组(array);
i:0指序列索引;
a:4指有4个字段;
s:8:"gameName"说明这是有8个字符的字符串(string)
总结: PHP的serialize将数组序列化后是便于存储,而JSON格式的数据不仅便于存储还能跟其他语言如javascript读取。如果前后端交互使用比较多的话建议使用JSON,结合PHP、Javascript、JSON以及Ajax就可以完成强大的数据交互功能。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The Gson@Expose annotation can be used to mark whether a field is exposed (contained or not) for serialization or deserialization. The @Expose annotation can take two parameters, each parameter is a boolean value and can take the value true or false. In order for GSON to react to the @Expose annotation, we have to create a Gson instance using the GsonBuilder class and need to call the excludeFieldsWithoutExposeAnnotation() method, which configures Gson to exclude all fields without Expose annotation from serialization or deserialization. Syntax publicGsonBuilderexclud

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.

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

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.

Quick Start: Pandas method of reading JSON files, specific code examples are required Introduction: In the field of data analysis and data science, Pandas is one of the important Python libraries. It provides rich functions and flexible data structures, and can easily process and analyze various data. In practical applications, we often encounter situations where we need to read JSON files. This article will introduce how to use Pandas to read JSON files, and attach specific code examples. 1. Installation of Pandas

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 to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

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
