search
Homephp教程php手册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就可以完成强大的数据交互功能。

Statement
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment