json_encode — 对变量进行 JSON 编码

4 篇文章 0 订阅

数据库结构:

数据库的数据:


index.php页面

<?php

mysql_connect('localhost','root','');
mysql_select_db('wen');
mysql_query("set names utf8");

$sql="select * from xian";
$result=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_array($result)){
$arr[]=$row;
}
echo json_encode($arr);

?>


ajax.html页面

<script src="jquery-1.7.2.js"></script>

<script>

$(document).ready(function(){

$.getJSON("index.php",function(data){

var con=null;

$.each(data,function(InfoIndex,Info){

con="名字:"+Info['name']+"<br>"+"标题:"+Info['title']+"<br>"+"内容:"+Info['content']+"<br>"+"时间:"+Info['date']+"<br><br>";

//alert(con);

$(".wen").append(con);

});

});

});

</script>

</head>

<body>

<div class="wen"></div>

</body>


在index.php页面输出的数据:

[{"0":"1","id":"1","1":"\u5c0f\u5c0f","name":"\u5c0f\u5c0f","2":"\u6539\u4e00\u6539\u5c0f\u6545\u4e8b","title":"\u6539\u4e00\u6539\u5c0f\u6545\u4e8b","3":"\u6709\u4e00\u5929\uff0c\u67d0\u67d0\u4eba\u53bb\u4e86\u67d0\u67d0\u5730\u65b9\u3002\u3002\u3002","content":"\u6709\u4e00\u5929\uff0c\u67d0\u67d0\u4eba\u53bb\u4e86\u67d0\u67d0\u5730\u65b9\u3002\u3002\u3002","4":"2013-06-03 10:38:21","date":"2013-06-03 10:38:21"},{"0":"2","id":"2","1":"\u5c0f\u7cfb","name":"\u5c0f\u7cfb","2":"\u5b8c\u4e86","title":"\u5b8c\u4e86","3":"\u653e\u5047\u4e86\uff0c\u6211\u4eec\u53bb\u90a3\u91cc\u73a9\u554a\uff01","content":"\u653e\u5047\u4e86\uff0c\u6211\u4eec\u53bb\u90a3\u91cc\u73a9\u554a\uff01","4":"2013-06-03 10:38:21","date":"2013-06-03 10:38:21"}]


在ajax.html页面显示的内容:

名字:小小

标题:改一改小故事

内容:有一天,某某人去了某某地方。。。

时间:2013-06-03 10:38:21


名字:小系

标题:完了

内容:放假了,我们去那里玩啊!

时间:2013-06-03 10:38:21


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值