Four formats of PHP output JSON JSON object JSON array JSON nesting

Time:2021-8-1

First: JSON array

<?php
//Declare header as JSON
header("Content-type:application/json");

//Build data
$data = array(
    array(
        'title '= >' Baidu ',
        'time'=>'2000',
        'website'=>'www.baidu.com'
    ),
    array(
        'title '= >' Alibaba ',
        'time'=>'1999',
        'website'=>'www.alibaba.com'
    ),
    array(
        'title '= >' Tencent ',
        'time'=>'1998',
        'website'=>'www.qq.com'
    )
);

//Convert to JSON
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>

Format returned

[  
    {  
        "Title": "Baidu",  
        "time":"2000",  
        "website":"www.baidu.com"  
    },  
    {  
        "Title": "Alibaba",  
        "time":"1999",  
        "website":"www.alibaba.com"  
    },  
    {  
        "Title": "Tencent",  
        "time":"1998",  
        "website":"www.qq.com"  
    }  
]

Second: JSON object

<?php
//Declare header as JSON
header("Content-type:application/json");

//Build data
$data = array(
        'title '= >' Baidu ',
        'time'=>'2000',
        'website'=>'www.baidu.com'
    );

//Convert to JSON
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>

Format returned

{  
 "Title": "Baidu",  
 "time":"2000",  
 "website":"www.baidu.com"  
}

The third: nested JSON objects

<?php
//Declare header as JSON
header("Content-type:application/json");

//Build data
$data = array(
        'title '= >' Alibaba ',
        'time'=>'1999',
        'website'=>'www.alibaba.com',
        'msg'=>array(
            'originator' = > 'ma Yun',
            'price' = > '584.27 billion US dollars',
            'sstime '= >' September 19, 2014 '
        )
    );

//Convert to JSON
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>

Format returned

{  
 "Title": "Alibaba",  
 "time":"1999",  
 "website":"www.alibaba.com",  
 "msg":{  
        "Originator": "Ma Yun",  
        "Price": "USD 584.27 billion",  
        "Sstime": "September 19, 2014"  
    }  
}

Fourth: nested arrays in JSON objects

<?php
//Declare header as JSON
header("Content-type:application/json");

//Build data
$data = array(
        'title '= >' Alibaba ',
        'time'=>'1999',
        'website'=>'www.alibaba.com',
        'msg'=>array(
            array(
                'product' = > 'Taobao',
                'url'=>'https://www.taobao.com'
            ),
            array(
                'product' = > 'tmall',
                'url'=>'https://www.tmall.com'
            ),
            array(
                'product'=>'1688',
                'url'=>'https://www.1688.com'
            )
        )
    );

//Convert to JSON
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>

Format returned

{  
 "Title": "Alibaba",  
 "time":"1999",  
 "website":"www.alibaba.com",  
 "msg":[  
        {  
            "Product": "Taobao",  
            "url":"https://www.taobao.com"  
        },  
        {  
            "Product": "tmall",  
            "url":"https://www.tmall.com"  
        },  
        {  
            "product":"1688",  
            "url":"https://www.1688.com"  
        }  
    ]  
}

Author:TANKING
Web:https://www.likeyunba.com/
Date:2020-06-03
WeChat:face6009

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]