Home  >  Article  >  Backend Development  >  JSON知多少-JSON数据结构_PHP教程

JSON知多少-JSON数据结构_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:29:37787browse


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

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

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

 


 
 然后用PHP的json_encode()函数,对一二维数组转换成JSON形式
 
 但转换出来的JSON形式:
 代码片断2:
 

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

 



看出来形式是不一致的。

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/771650.htmlTechArticle最近在开发微信平台,要使用JSON进行数据交换,之前用过JSON,但仅限于 在开发微信平台中,要使用JSON形式如下: 代码片断1: { "button":...
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