[PHP典型模塊與項目實戰大全]-第四章(json教學)
[PHP典型模塊與項目實戰大全]-第四章(json教學)
code2html:http://tohtml.com/
標準JSON產生
<?php
//http://blog.wu-boy.com/2011/04/%E4%BD%A0%E4%B8%8D%E5%8F%AF%E4%B8%8D%E7%9F%A5%E7%9A%84-json-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%B4%B9/
$cart=array(
"orderID"=>12345,
"shopperName"=>"John Smith",
"shopperEmail"=>"johnsmith@example.com",
"contents"=>array(
array(
"productID"=>34,
"productName"=>"SuperWidget",
"quantity"=>1
),
array(
"productID"=>56,
"productName"=>"WonderWidget",
"quantity"=>3
)
),
"orderCompleted"=>true
);
echo json_encode($cart);
?>
|
標準JSON解析
<?php
//http://blog.wu-boy.com/2011/04/%E4%BD%A0%E4%B8%8D%E5%8F%AF%E4%B8%8D%E7%9F%A5%E7%9A%84-json-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%B4%B9/
$jsonString='
{
"orderID": 12345,
"shopperName": "John Smith",
"shopperEmail": "johnsmith@example.com",
"contents": [
{
"productID": 34,
"productName": "SuperWidget",
"quantity": 1
},
{
"productID": 56,
"productName": "WonderWidget",
"quantity": 3
}
],
"orderCompleted": true
}
';
$cart=json_decode($jsonString);
echo$cart->shopperEmail."<br>";
echo$cart->contents[1]->productName."<br>";
?>
|
WEB動態產生JSON
<?php
header('Content-Type: application/json');
$arraydata=array();
for($i=0;$i<5;$i++)
{
$arraydata[$i]["name"]="jash";
$arraydata[$i]["job"]="sw";
}
echo json_encode($arraydata);
?>
|