Home > php教程 > php手册 > body text

PHP如何返回json格式的数据给jquery

WBOY
Release: 2016-05-25 16:48:07
Original
907 people have browsed it

json格式的数据是我们在应用开发中一直会使用到的数据,如与jquery打交到或与api打交都会使用到json数据,那么PHP如何返回json格式的数据给jquery呢,下面我来给各位同学介绍介绍.

在jquery中操作json数据我们直接 $.parseJSON(returnString ) 了

实例代码如下:

$(function () {
	$('#send').click(function () {
		$.getJSON('test.js', function (data) {
			$('#resText').emptyempty();
			var html = '';
			$.each(data, function (commentIndex, comment) {
				html += &#39;<div class="comment"><h6>&#39; + comment[&#39;username&#39;] + &#39;:</h6><p class="para">&#39; + comment[&#39;content&#39;] + &#39;</p></div>&#39;;
			})
			$(&#39;#resText&#39;).html(html);
		})
	})
})
Copy after login

你需要做的就是将数据存储为格式正确的 .json或者.js 文件.以下为示例所传送的json格式的数据

实例代码如下:

[ 
  { 
	"username": "张三", 
	"content": "沙发." 
  }, 
  { 
	"username": "李四", 
	"content": "板凳." 
  }, 
  { 
	"username": "王五", 
	"content": "地板." 
  } 
]
Copy after login

上面讲到到的json数据是固定了,我们用php如何返回json数据呢

php输出JSON格式方法

页面中加入header('Content-type: text/json');这个头就是告知此文件输出类型为 json,这种形式我们见的最多的是验证码——php输出验证图片,有时php可以输出css文件,js文件等做一些有趣的事情.好的,我们测试一下吧

实例代码如下:

<?php 
header(&#39;Content-type: text/json&#39;); 
$fruits = array ( 
	"fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"), 
	"numbers" => array(1, 2, 3, 4, 5, 6), 
	"holes"   => array("first", 5 => "second", "third") 
); 
echo json_encode($fruits); 
?>
Copy after login

从数据库读取的数据生成json格式

实例代码如下:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>第一php网提供的教程--将数据库读取的数据生成json格式</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="http://libs.useso.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"/></script> 
<script language=javascript> 
</script> 
</head> 
<body> 
<pre class="brush:php;toolbar:false"> 
<h1>请注意两种方法生成的对象数组在结构上的区别</h1> 
<?php 
echo &#39;<h1>法一</h1>&#39;; 
//假设以下数组是根据我们从数据库读取的数据生成的 
$jarr=array(&#39;total&#39;=>239,&#39;row&#39;=>array( 
	   array(&#39;code&#39;=>&#39;001&#39;,&#39;name&#39;=>&#39;中国&#39;,&#39;addr&#39;=>&#39;Address 11&#39;,&#39;col4&#39;=>&#39;col4 data&#39;), 
	   array(&#39;code&#39;=>&#39;002&#39;,&#39;name&#39;=>&#39;Name 2&#39;,&#39;addr&#39;=>&#39;Address 12&#39;,&#39;col4&#39;=>&#39;col4 data&#39;), 
	) 
); 
//法一: 
$jobj=new stdclass();//实例化stdclass,这是php内置的空类,可以用来传递数据,由于json_decode后的数据是以对象数组的形式存放的, 
//所以我们生成的时候也要把数据存储在对象中 
foreach($jarr as $key=>$value){ 
$jobj->$key=$value; 
} 
print_r($jobj);//打印传递属性后的对象 
echo &#39;使用$jobj->row[0][&#39;code&#39;]输出数组元素:&#39;.$jobj->row[0][&#39;code&#39;].&#39;<br>&#39;; 
echo &#39;编码后的json字符串:&#39;.json_encode($jobj).&#39;<br>&#39;;//打印编码后的json字符串 
//法二: 
echo &#39;<hr>&#39;; 
echo &#39;<h1>法二</h1>&#39;; 
echo &#39;编码后的json字符串:&#39;; 
echo $str=json_encode($jarr);//将数组进行json编码 
echo &#39;<br>&#39;; 
$arr=json_decode($str);//再进行json解码 
print_r($arr);//打印解码后的数组,数据存储在对象数组中 
echo &#39;使用$arr->row[0]->code输出数组元素:&#39;.$arr->row[0]->code; 
?>
</body> 
</html>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!