首頁 > php教程 > php手册 > 主體

JSON格式化与serialize序列化 - 璐小璐

WBOY
發布: 2016-05-20 10:14:32
原創
3637 人瀏覽過

一、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" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="xml"><span class="php"><span class="hljs-preprocessor" style="color: #f99b15;"><?php </span>
    <span class="hljs-variable" style="color: #ef6155;">$arr</span> = <span class="hljs-keyword" style="color: #815ba4;">array</span>(
        <span class="hljs-string" style="color: #48b685;">'name'</span>    =<span class="hljs-string" style="color: #48b685;">'刘璐'</span>,
        <span class="hljs-string" style="color: #48b685;">'nick'</span>    =<span class="hljs-string" style="color: #48b685;">'璐小璐'</span>,
        <span class="hljs-string" style="color: #48b685;">'age'</span>     =<span class="hljs-string" style="color: #48b685;">'26'</span>,``
        <span class="hljs-string" style="color: #48b685;">'contact'</span> =<span class="hljs-keyword" style="color: #815ba4;">array</span>(
            <span class="hljs-string" style="color: #48b685;">'phone'</span>   =<span class="hljs-string" style="color: #48b685;">'13718136109'</span>,
            <span class="hljs-string" style="color: #48b685;">'address'</span> =<span class="hljs-string" style="color: #48b685;">'Beijing ifdoo'</span>
        )
    );

    <span class="hljs-variable" style="color: #ef6155;">$str</span> = json_encode(<span class="hljs-variable" style="color: #ef6155;">$arr</span>);
    <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"getProfile($str)"</span>;
<span class="hljs-preprocessor" style="color: #f99b15;">?></span></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" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><script class="hljs-typedef"><span class="hljs-keyword" style="color: #815ba4;">type="text/javascript>
    var arr = {<span class="hljs-string" style="color: #48b685;">"name":<span class="hljs-string" style="color: #48b685;">"\u5218\u7490",<span class="hljs-string" style="color: #48b685;">"nick":<span class="hljs-string" style="color: #48b685;">"\u7490\u5c0f\u7490",<span class="hljs-string" style="color: #48b685;">"age":<span class="hljs-string" style="color: #48b685;">"26",<span class="hljs-string" style="color: #48b685;">"contact":{<span class="hljs-string" style="color: #48b685;">"phone":<span class="hljs-string" style="color: #48b685;">"13718136109",<span class="hljs-string" style="color: #48b685;">"address":<span class="hljs-string" style="color: #48b685;">"\u5317\u4eac \u5f97\u8c46"}};
    alert(arr.name);  
</script>  
</code>
登入後複製

4. 实例

index.html

<code class="hljs vbscript-html" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="xml"><span class="hljs-tag" style="color: #ef6155;">html</span>></span>
<span class="hljs-tag" style="color: #ef6155;">head</span>>
    <span class="hljs-tag" style="color: #ef6155;">title</span>>json demo<span class="hljs-tag" style="color: #ef6155;"><span class="hljs-title" style="color: #ef6155;">title</span>></span>
    <span class="hljs-tag" style="color: #ef6155;">script</span> <span class="hljs-attribute" style="color: #ef6155;">type</span>=<span class="hljs-value" style="color: #48b685;">"text/javascript"</span>><span class="javascript" style="opacity: 0.5;">
    <span class="hljs-function" style="color: #815ba4;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #06b6ef;">getProfile</span><span class="hljs-params" style="color: #f99b15;">(str)</span> </span>{
        <span class="hljs-keyword" style="color: #815ba4;">var</span> arr = str;
        <span class="hljs-built_in" style="color: #f99b15;">document</span>.getElementById(<span class="hljs-string" style="color: #48b685;">'nick'</span>).innerHTML = arr.nick;
    }
    </span><span class="hljs-tag" style="color: #ef6155;"><span class="hljs-title" style="color: #ef6155;">script</span>></span>
<span class="hljs-tag" style="color: #ef6155;"><span class="hljs-title" style="color: #ef6155;">head</span>></span>
<span class="hljs-tag" style="color: #ef6155;">body</span>>
    <span class="hljs-tag" style="color: #ef6155;">div</span> <span class="hljs-attribute" style="color: #ef6155;">id</span>=<span class="hljs-value" style="color: #48b685;">"nick"</span>><span class="hljs-tag" style="color: #ef6155;"><span class="hljs-title" style="color: #ef6155;">div</span>></span>
<span class="hljs-tag" style="color: #ef6155;"><span class="hljs-title" style="color: #ef6155;">body</span>></span>

<span class="hljs-tag" style="color: #ef6155;">script</span> <span class="hljs-attribute" style="color: #ef6155;">type</span>=<span class="hljs-value" style="color: #48b685;">"text/javascript"</span> <span class="hljs-attribute" style="color: #ef6155;">src</span>=<span class="hljs-value" style="color: #48b685;">"./profile.php"</span>><span class="hljs-tag" style="color: #ef6155;"><span class="hljs-title" style="color: #ef6155;">script</span>></span>
<span class="hljs-tag" style="color: #ef6155;"><span class="hljs-title" style="color: #ef6155;">html</span>></span>
</code>
登入後複製

profile.php

<code class="hljs vbscript-html" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="xml"><span class="php"><span class="hljs-preprocessor" style="color: #f99b15;"><?php </span>
    <span class="hljs-variable" style="color: #ef6155;">$arr</span> = <span class="hljs-keyword" style="color: #815ba4;">array</span>(
        <span class="hljs-string" style="color: #48b685;">'name'</span>    => <span class="hljs-string" style="color: #48b685;">'刘璐'</span>,
        <span class="hljs-string" style="color: #48b685;">'nick'</span>    => <span class="hljs-string" style="color: #48b685;">'璐小璐'</span>,
        <span class="hljs-string" style="color: #48b685;">'age'</span>     => <span class="hljs-string" style="color: #48b685;">'26'</span>,
        <span class="hljs-string" style="color: #48b685;">'contact'</span> => <span class="hljs-keyword" style="color: #815ba4;">array</span>(
            <span class="hljs-string" style="color: #48b685;">'phone'</span>   => <span class="hljs-string" style="color: #48b685;">'13718136109'</span>,
            <span class="hljs-string" style="color: #48b685;">'address'</span> => <span class="hljs-string" style="color: #48b685;">'Beijing ifdoo'</span>
        )
    );

    <span class="hljs-variable" style="color: #ef6155;">$str</span> = json_encode(<span class="hljs-variable" style="color: #ef6155;">$arr</span>);
    <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"getProfile($str)"</span>;
<span class="hljs-preprocessor" style="color: #f99b15;">?></span></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" style="background-color: #d6dbdf; border: 0; border-radius: 4px; color: #4f424c; font-size: 90%; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="xml"><span class="php"><span class="hljs-preprocessor" style="color: #f99b15;"><?php </span>
    <span class="hljs-variable" style="color: #ef6155;">$arr</span> = <span class="hljs-keyword" style="color: #815ba4;">array</span>( 
        <span class="hljs-string" style="color: #48b685;">"u1"</span> => <span class="hljs-keyword" style="color: #815ba4;">array</span>( 
            <span class="hljs-string" style="color: #48b685;">"gameName"</span> => <span class="hljs-string" style="color: #48b685;">"德乙"</span>, 
            <span class="hljs-string" style="color: #48b685;">"homeName"</span> => <span class="hljs-string" style="color: #48b685;">"比勒费尔德"</span>, 
            <span class="hljs-string" style="color: #48b685;">"guestName"</span> => <span class="hljs-string" style="color: #48b685;">"不伦瑞克"</span>, 
            <span class="hljs-string" style="color: #48b685;">"endTime"</span> => <span class="hljs-string" style="color: #48b685;">"2015-08-21"</span> 
        ), 
        <span class="hljs-string" style="color: #48b685;">"u2"</span> => <span class="hljs-keyword" style="color: #815ba4;">array</span>( 
            <span class="hljs-string" style="color: #48b685;">"gameName"</span> => <span class="hljs-string" style="color: #48b685;">"英超"</span>, 
            <span class="hljs-string" style="color: #48b685;">"homeName"</span> => <span class="hljs-string" style="color: #48b685;">"水晶宫"</span>, 
            <span class="hljs-string" style="color: #48b685;">"guestName"</span> => <span class="hljs-string" style="color: #48b685;">"阿斯顿维拉"</span>, 
            <span class="hljs-string" style="color: #48b685;">"endTime"</span> => <span class="hljs-string" style="color: #48b685;">"2015-08-22"</span> 
        ) 
    ); 
    <span class="hljs-keyword" style="color: #815ba4;">echo</span> serialize(<span class="hljs-variable" style="color: #ef6155;">$arr</span>);
<span class="hljs-preprocessor" style="color: #f99b15;">?></span></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就可以完成强大的数据交互功能。

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板