zl程序教程

您现在的位置是:首页 >  后端

当前栏目

PHP6先修班JSON实例代码

2023-06-13 09:14:01 时间
它是基於JavaScriptProgrammingLanguage,StandardECMA-2623rdEdition-December1999的一??子集
JSON主要利用了成?Φ{}?戆?「??object(物件),用成?Φ[]?戆???array(?列),
用成?Φ""?戆?「髯执??枚禾???^隔各??刀?Y料型?B有string,number,array,object

下面??蔚?SON格式,?⑹隽艘??objectjson?碛幸??成?T??????成?T??抵杏泻?腥??物件

复制内容到剪贴板代码:
varjson={
"query":[
{"id":"1","type":"a","title":"PHP5.2.0的新功能JSONdecoder&encoder"},
{"id":"2","type":"b","title":"JSON全?JavaScriptObjectNotation"},
{"array":["A","B","C","D","E"]}
]
};
如此,我??可以?得一??叫做json的Object,而???jsonObject中包含一???立的成?Tquery
而query包含一??Array,???Array中又含了三??Object,前面二??Object含有三??成?T
id,type,title,而最後一??Objectarray包含一???列,如此解??明白吧?

但是要怎?用呢?
很??
alert("Ihave"+json.query.length+"object.");
//alertIhave3object.
alert("type="+json.query[1].type+"\r\ntitle"+json.query[1].title);
//alerttype=btitle=JSON全?JavaScriptObjectNotation
alert("?列索引3="+json.query[2].array[3]);
//alert?列索引3=D

??硬僮髻Y料?r更?便,不需要和??的DOM打交道,所需要的?料可以很??的取得
例如上面的例子json.query[i].title如此就可以取得第i?的title?群?闹
PHP的?展是很迅速,?程式界??SON?一知半解?r或者全然不知何??SON?r
PHP已?在最新的版本5.2.0中?入核心,?K且????B是?⒂茫?噍^於其他的Script?言
PHP可?一??先,在5.2.0版本中??SON??作了???函?json_decode()和json_encode()
前者是??SON格式的字串?原成PHP原生的?列
後者?t是??HP原生?列??成JSON格式的字串
不?,由於Javascript支援Unicode,如果在存取?料??r使用非Ascii的字元,如中、日、?
需要?⒆衷?????Q成UTF8,不然??json_encode()後的字串???y?
========================================================
??上一篇??谓榻BJOSN後
本篇就???作如何使用JOSN
下面?例使用需要使用MySQL4.1以上版本
??全程?裼?tf8
承接上一篇的?料格式,表中共有三???谖?d,type,title
?料表?格如下
复制内容到剪贴板代码:
CREATETABLE`news`(
`id`int(10)unsignedNOTNULLauto_increment,
`type`varchar(255)NOTNULLdefault"",
`title`varchar(64)NOTNULLdefault"",
PRIMARYKEY(`id`)
)ENGINE=MyISAMAUTO_INCREMENT=0DEFAULTCHARSET=utf8;
复制内容到剪贴板代码:
<?php
//建立??
$conn=mysqli_connect("localhost","root","")ordie("?不上?料??);
//??褓Y料?
mysqli_select_db($conn,"mydata")ordie("不能??料??);
//?定??????t,不懂上google找
mysqli_query($conn,"SETNAMES"utf8"");
//取出?料
$results=mysqli_query($conn,"SELECTid,type,titleFROMnews");
//Josn字串
$json="";
//因?槭枪?例,所以自行控制?圈
$i=0;
while($row=mysqli_fetch_assoc($results))
{
$i++;
$json.=json_encode($row);
//?料表中只放三??料,所以在第三??r不需要在尾巴加上",",?得,最後一??料不用加上","
if($i<3)
{
$json.=",";
}

}
//?①Y料包??列中
$json="{"query":[".$json."]}";?>
<!doctypehtmlpublic"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"dir="ltr"xml:lang="zh-tw"lang="zh-tw">
<head>
<title>Json?例</title>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<metahttp-equiv="Pragma"content="no-cache"/>
<metahttp-equiv="Expires"content="-1"/>
<metahttp-equiv="Cache-Control"content="no-cache"/>
<metaname="generator"content="mamba"/>
</head>
<body>
<scripttype="text/javascript">
varjson=<?phpecho$json?>;
alert("Ihave"+json.query.length+"object.");
alert("type="+json.query[1].type+"rntitle"+json.query[1].title);
//上一篇?介中使用?
</script>
?原Json<br>
<?php
//?⒆执?獯a
$s_JSON_Decoded=json_decode($json,true);
//取回?料
foreach($s_JSON_Decodedas$row)
{
foreach($rowas$rowa)
{
echo$rowa["title"]."<br>";
}

}
?>
</body>
</html>
????蔚难菥?後
相信大家??SON?玩意有更深一?拥牟t解
?然JSON的??貌恢皇枪?例中那???
有?趣一起研究吧