Table of Contents
JSON formatting and serialize serialization, jsonserialize
1. JSON formatting
2. serialize serialization
Home Backend Development PHP Tutorial JSON formatting and serialize serialization, jsonserialize_PHP tutorial

JSON formatting and serialize serialization, jsonserialize_PHP tutorial

Jul 12, 2016 am 08:53 AM
json

JSON formatting and serialize serialization, jsonserialize

1. JSON formatting

1. What is JSON

JSON is a data storage format used to communicate the interaction between client-side Javascript and server-side PHP. We pass the string generated by JSON using PHP to the front-end Javascript, and Javascirpt can easily decode it into JSON and then apply it.

2. How to use JSON

PHP can use two functions, json_encode() and json_decode(), to operate JSON - one for encoding and one for decoding. json_encode() can convert an array into text data in json format for easy storage and reading, while json_decode() can directly convert json data into an array for easy calling.

<code class="hljs vbscript-html"><span class="xml"><span class="php"><span class="hljs-preprocessor">&lt;?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">?&gt;</span></span>
</span></code>
Copy after login

The result is:
{"name":"u5218u7490","nick":"u7490u5c0fu7490","age":"26","contact":{"phone":"13718136109","address":"u5317u4eac u5f97u8c46"}}

3. JSON format data and WEB front-end JS complete asynchronous interaction process

After PHP uses json_encode() to convert the array into json format data, this json string is equivalent to an array in JavaScript. After assigning it to a variable, you can operate on the array.

<code class="hljs haskell">&lt;script <span class="hljs-typedef"><span class="hljs-keyword">type</span>="text/javascript&gt;</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);  
&lt;/script&gt;  
</code>
Copy after login

4. Example

index.html

<code class="hljs vbscript-html"><span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>json demo<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">script</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/javascript"</span>&gt;</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">&lt;/<span class="hljs-title">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"nick"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>

<span class="hljs-tag">&lt;<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>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
</span></code>
Copy after login

profile.php

<code class="hljs vbscript-html"><span class="xml"><span class="php"><span class="hljs-preprocessor">&lt;?php</span>
    <span class="hljs-variable">$arr</span> = <span class="hljs-keyword">array</span>(
        <span class="hljs-string">'name'</span>    =&gt; <span class="hljs-string">'刘璐'</span>,
        <span class="hljs-string">'nick'</span>    =&gt; <span class="hljs-string">'璐小璐'</span>,
        <span class="hljs-string">'age'</span>     =&gt; <span class="hljs-string">'26'</span>,
        <span class="hljs-string">'contact'</span> =&gt; <span class="hljs-keyword">array</span>(
            <span class="hljs-string">'phone'</span>   =&gt; <span class="hljs-string">'13718136109'</span>,
            <span class="hljs-string">'address'</span> =&gt; <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">?&gt;</span></span>
</span></code>
Copy after login
  • HTML page calls PHP file

    &lt;script language="text/javascript" src="/xx/a.php"&gt;&lt;/script&gt;

    The echo in a.php outputs javascript code.

  • php page calls js file

    The method in echo js in a.php is enough.

2. serialize serialization

1. What is serialize

Serialize is to serialize variables and return a string expression with variable type and structure.

2. How to use serialize

Use PHP's serialize and unserialize to serialize and deserialize arrays.

<code class="hljs vbscript-html"><span class="xml"><span class="php"><span class="hljs-preprocessor">&lt;?php</span>
    <span class="hljs-variable">$arr</span> = <span class="hljs-keyword">array</span>( 
        <span class="hljs-string">"u1"</span> =&gt; <span class="hljs-keyword">array</span>( 
            <span class="hljs-string">"gameName"</span> =&gt; <span class="hljs-string">"德乙"</span>, 
            <span class="hljs-string">"homeName"</span> =&gt; <span class="hljs-string">"比勒费尔德"</span>, 
            <span class="hljs-string">"guestName"</span> =&gt; <span class="hljs-string">"不伦瑞克"</span>, 
            <span class="hljs-string">"endTime"</span> =&gt; <span class="hljs-string">"2015-08-21"</span> 
        ), 
        <span class="hljs-string">"u2"</span> =&gt; <span class="hljs-keyword">array</span>( 
            <span class="hljs-string">"gameName"</span> =&gt; <span class="hljs-string">"英超"</span>, 
            <span class="hljs-string">"homeName"</span> =&gt; <span class="hljs-string">"水晶宫"</span>, 
            <span class="hljs-string">"guestName"</span> =&gt; <span class="hljs-string">"阿斯顿维拉"</span>, 
            <span class="hljs-string">"endTime"</span> =&gt; <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">?&gt;</span></span>
</span></code>
Copy after login

The result is:

a:2:{s:2:"u1";a:4:{s:8:"gameName";s:6:"Germany 2";s:8:"homeName";s:15: "Bielefeld";s:9:"guestName";s:12:"Brunswick";s:7:"endTime";s:10:"2015-08-21";}s: 2:"u2";a:4:{s:8:"gameName";s:6:"Premier League";s:8:"homeName";s:9:"Crystal Palace";s:9:"guestName" ;s:15:"Aston Villa";s:7:"endTime";s:10:"2015-08-22";}}

Among them:

a:2 indicates that this is an array with two elements;
i:0 refers to the sequence index;
a:4 means there are 4 fields;
s:8:"gameName" indicates that this is an 8-character string

Summary: PHP's serialize serializes arrays for easy storage, and data in JSON format is not only easy to store but can also be read with other languages ​​such as javascript. If there is a lot of front-end and back-end interaction, it is recommended to use JSON. Combining PHP, Javascript, JSON and Ajax can complete powerful data interaction functions.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1125519.htmlTechArticleJSON formatting and serialize serialization, jsonserialize 1. JSON formatting 1. What is JSON? JSON is a kind of data The storage format is used to communicate the interaction between client-side Javascript and server-side PHP. ...
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 Article Tags

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)

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

Combination of golang WebSocket and JSON: realizing data transmission and parsing

How to exclude a field from JSON using @Expose annotation in Java? How to exclude a field from JSON using @Expose annotation in Java? Sep 16, 2023 pm 09:49 PM

How to exclude a field from JSON using @Expose annotation in Java?

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

What is the difference between MySQL5.7 and MySQL8.0?

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 tips for converting PHP arrays to JSON

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development

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

Pandas usage tutorial: Quick start for reading JSON files Pandas usage tutorial: Quick start for reading JSON files Jan 13, 2024 am 10:15 AM

Pandas usage tutorial: Quick start for reading JSON files

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

How do annotations in the Jackson library control JSON serialization and deserialization?

See all articles