Home > php教程 > php手册 > body text

JSON格式化与serialize序列化,jsonserialize

WBOY
Release: 2016-06-13 08:40:26
Original
1616 people have browsed it

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>
Copy after login

结果为:
{"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>
Copy after login

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>
Copy after login

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>
Copy after login
  • 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>
Copy after login

结果为:

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就可以完成强大的数据交互功能。

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template