当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP6 先修班 JSON实例代码

PHP6 先修班 JSON实例代码

2019年05月10日  | 移动技术网IT编程  | 我要评论

大名县政府,mm小游戏h,怪诞心理学2

它是基於javascript programming language, standard ecma-262 3rd edition - december 1999的一個子集
json 主要利用了成對的 {} 來包住各個object(物件),用成對的 [] 來包各個array(陣列),
用成對的 "" 來包住各字串,用逗號來區隔各變數而資料型態有 string, number, array, object

下面簡單的json格式,敘述了一個 object json 擁有一個成員變數,這個成員變數中有含有三個物件

复制内容到剪贴板代码:
var json = {
'query' : [
{'id':'1','type':'a','title':'php 5.2.0 的新功能 json decoder & encoder'},
{'id':'2','type':'b','title':'json 全稱 javascript object notation'},
{'array': ['a', 'b','c', 'd', 'e']}
]
};
如此,我們可以獲得一個叫做 json 的object,而這個json object中包含一個獨立的成員 query
而query包含一個array ,這個array中又含了三個object,前面二個object含有三個成員
id,type,title,而最後一個object array 包含一個陣列,如此解釋還明白吧?

但是要怎麼用呢?
很簡單
alert('i have ' +json.query.length + ' object.');
//alert i have 3 object.
alert('type='+json.query[1].type+'\r\ntitle'+json.query[1].title);
//alert type=b title=json 全稱 javascript object notation
alert('陣列索引3='+json.query[2].array[3]);
//alert 陣列索引3=d

這樣操作資料時更簡便,不需要和複雜的dom打交道,所需要的資料可以很輕鬆的取得
例如上面的例子 json.query[ i ].title 如此就可以取得第i筆的title內含的值
php的發展是很迅速,當程式界對json還一知半解時或者全然不知何為json時
php已經在最新的版本5.2.0中納入核心,並且預設狀態是啟用,相較於其他的script語言
php可謂一馬當先,在5.2.0版本中為json實作了兩個函數 json_decode() 和 json_encode()
前者是將json格式的字串還原成php原生的陣列
後者則是將php原生陣列編譯成json格式的字串
不過,由於javascript支援unicode,如果在存取資料庫時使用非ascii的字元,如中、日、韓
需要將字元編碼轉換成utf8,不然經過json_encode()後的字串會是亂碼
========================================================
經過上一篇簡單介紹josn後
本篇就來實作如何使用josn
下面範例使用需要使用mysql4.1以上版本
編碼全程採用utf8
承接上一篇的資料格式,表中共有三個欄位id,type,title
資料表規格如下
复制内容到剪贴板代码:
create table `news` (
`id` int(10) unsigned not null auto_increment,
`type` varchar(255) not null default '',
`title` varchar(64) not null default '',
primary key (`id`)
) engine=myisam auto_increment=0 default charset=utf8;
复制内容到剪贴板代码:
<?php
//建立連線
$conn = mysqli_connect("localhost", 'root', '')or die('連不上資料庫');
//選擇資料庫
mysqli_select_db($conn,'mydata') or die('不能選資料庫');
//設定連線編碼規則,不懂上google找
mysqli_query($conn,'set names 'utf8'');
//取出資料
$results = mysqli_query($conn,'select id,type,title from news');
//josn字串
$json = '';
//因為是範例,所以自行控制迴圈
$i=0;
while($row = mysqli_fetch_assoc($results))
{
$i++;
$json .= json_encode($row);
//資料表中只放三筆資料,所以在第三筆時不需要在尾巴加上 ",",記得,最後一筆資料不用加上","
if ($i<3)
{
$json .= ",";
}

}
//將資料包進陣列中
$json = '{"query":[ '.$json.']}';?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="zh-tw" lang="zh-tw" >
<head>
<title>json範例</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<meta http-equiv="cache-control" content="no-cache" />
<meta name="generator" content="mamba" />
</head>
<body>
<script type="text/javascript">
var json = <?php echo $json?>;
alert('i have ' +json.query.length + ' object.');
alert('type='+json.query[1].type+'rntitle'+json.query[1].title);
//上一篇簡介中使用過
</script>
還原json<br>
<?php
//將字串解碼
$s_json_decoded = json_decode($json,true);
//取回資料
foreach ($s_json_decoded as $row)
{
foreach ($row as $rowa)
{
echo $rowa['title']."<br>";
}

}
?>
</body>
</html>
經過簡單的演練後
相信大家對json這玩意有更深一層的瞭解
當然json的應用不只是範例中那麼簡單
有興趣一起研究吧

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网