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

JSON知多少-JSON数据结构

WBOY
Release: 2016-06-13 09:35:01
Original
1382 people have browsed it


最近在开发微信平台,要使用JSON进行数据交换,之前用过JSON,但仅限于……

在开发微信平台中,要使用JSON形式如下:
代码片断1:

<span> {
     </span>"button":<span>[
     {    
          </span>"type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"<span>
      }</span>,<span>
      {
           </span>"type":"click",
           "name":"歌手简介",
           "key":"V1001_TODAY_SINGER"<span>
      }</span>,<span>
      {
           </span>"name":"菜单",
           "sub_button":<span>[
           {    
               </span>"type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"<span>
            }</span>,<span>
            {
               </span>"type":"view",
               "name":"视频",
               "url":"http://v.qq.com/"<span>
            }</span>,<span>
            {
               </span>"type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"<span>
            }]
       }]
 }</span>
Copy after login



然后用PHP的json_encode()函数,对一二维数组转换成JSON形式

但转换出来的JSON形式:
代码片断2:

<span>{
    </span>"button":<span> {
        </span>"1":<span> {
            </span>"type": "click",
            "name": "今日歌曲",
            "key": "V1001_TODAY_MUSIC"<span>
        }</span>,
        "2":<span> {
            </span>"type": "click",
            "name": "歌手简介",
            "key": "V1001_TODAY_SINGER"<span>
        }</span>,
        "3":<span> {
            </span>"name": "菜单",
            "sub_button":<span> [
                {
                    </span>"type": "view",
                    "name": "搜索",
                    "url": "http://www.soso.com/"<span>
                }</span>,<span>
                {
                    </span>"type": "view",
                    "name": "视频",
                    "url": "http://v.qq.com/"<span>
                }</span>,<span>
                {
                    </span>"type": "click",
                    "name": "赞一下我们",
                    "key": "V1001_GOOD"<span>
                }
            ]
        }
    }
}</span>
Copy after login

 



看出来形式是不一致的。

只能来了解一下JSON的结构形式。
JSON有两种数据:1.无序的对象结构;2.有序的数组结构
1.无序的对象结构
 无序的对象结构在不同语言中称法不同,比如在Python中称为字典,在JS中称为JSON对象……
 总之就是键/值对组合形式。
 刚才我转换出来的JSON结构就是无序的键/值对组合
2.有序的数组结构
 有序的的数组结构,即代码片断2所展示的结构。
 将数组作为有序数组进行转换JSON,就可以得到有序的JOSN数组结构。

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