PHP如何返回json格式的数据给jquery
May 25, 2016 pm 04:48 PMjson格式的数据是我们在应用开发中一直会使用到的数据,如与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 += '<div class="comment"><h6 id="nbsp-nbsp-comment-username-nbsp-nbsp">' + comment['username'] + ':</h6><p class="para">' + comment['content'] + '</p></div>'; }) $('#resText').html(html); }) }) })
你需要做的就是将数据存储为格式正确的 .json或者.js 文件.以下为示例所传送的json格式的数据
实例代码如下:
[ { "username": "张三", "content": "沙发." }, { "username": "李四", "content": "板凳." }, { "username": "王五", "content": "地板." } ]
上面讲到到的json数据是固定了,我们用php如何返回json数据呢
php输出JSON格式方法
页面中加入header('Content-type: text/json');这个头就是告知此文件输出类型为 json,这种形式我们见的最多的是验证码——php输出验证图片,有时php可以输出css文件,js文件等做一些有趣的事情.好的,我们测试一下吧
实例代码如下:
<?php header('Content-type: text/json'); $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); ?>
从数据库读取的数据生成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 id="请注意两种方法生成的对象数组在结构上的区别">请注意两种方法生成的对象数组在结构上的区别</h1> <?php echo '<h1 id="法一">法一</h1>'; //假设以下数组是根据我们从数据库读取的数据生成的 $jarr=array('total'=>239,'row'=>array( array('code'=>'001','name'=>'中国','addr'=>'Address 11','col4'=>'col4 data'), array('code'=>'002','name'=>'Name 2','addr'=>'Address 12','col4'=>'col4 data'), ) ); //法一: $jobj=new stdclass();//实例化stdclass,这是php内置的空类,可以用来传递数据,由于json_decode后的数据是以对象数组的形式存放的, //所以我们生成的时候也要把数据存储在对象中 foreach($jarr as $key=>$value){ $jobj->$key=$value; } print_r($jobj);//打印传递属性后的对象 echo '使用$jobj->row[0]['code']输出数组元素:'.$jobj->row[0]['code'].'<br>'; echo '编码后的json字符串:'.json_encode($jobj).'<br>';//打印编码后的json字符串 //法二: echo '<hr>'; echo '<h1 id="法二">法二</h1>'; echo '编码后的json字符串:'; echo $str=json_encode($jarr);//将数组进行json编码 echo '<br>'; $arr=json_decode($str);//再进行json解码 print_r($arr);//打印解码后的数组,数据存储在对象数组中 echo '使用$arr->row[0]->code输出数组元素:'.$arr->row[0]->code; ?> </body> </html>

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Astuces PHP : implémentez rapidement la fonction de retour à la page précédente

Quelle est la différence entre MySQL5.7 et MySQL8.0 ?

Combinaison de golang WebSocket et JSON : réalisation de la transmission et de l'analyse des données

Conseils d'optimisation des performances pour la conversion de tableaux PHP en JSON

Comment gérer les formats de données XML et JSON dans le développement C#

Comment les annotations de la bibliothèque Jackson contrôlent-elles la sérialisation et la désérialisation JSON ?

Utilisez la fonction json.MarshalIndent dans Golang pour convertir la structure en une chaîne JSON formatée

Tutoriel d'utilisation de Pandas : Démarrage rapide pour la lecture de fichiers JSON
