AJAX - JSON对象表示法

来源:互联网 发布:死神结局是什么 知乎 编辑:程序博客网 时间:2024/05/16 08:42

application/json


 JSON:JavaScript Object Notation,

JS对象表示法,JSON是一个字符串格式,用于描述数据,数据交换;

 

 JSON语法


表示数组的字符串:[ 10,"Tom",true, "2015-10-11" ]

表示对象的字符串:{ "ename":"Mary", "age":20 }


   (1)一段JSON字符串数据要么是“一个数组”,要么是“一个对象”——不能是多个!

(2)JSON字符串中对象属性、string数据都只能使用双引号

  

JSON字符串在项目中的使用方法:


服务器端

客户端

header('Content-Type:application/json;charset=UTF-8');

$arr = [ [ ],[ ],[ ] ];

$str = json_encode($arr); 

           //把PHP数组编码为JSON字符串

echo $str;

JSON.parse( xhr.responseText );  

//把JSON字符串解析为JS数组/对象

 

AJAX5种数据格式交互方法:


服务器端

客户端

header('Content-Type: text/plain');

echo '一段文本';

xhr.responseText

 

header('Content-Type: text/html');

echo '<tr><td>唐模</td></tr>';

tbody.innerHTML = xhr.responseText

header('Content-Type: application/javascript');

echo 'alert("添加成功")';

eval( xhr.responseText )

header('Content-Type: application/xml');

echo '<empList><emp></emp></empList>';

var document =xhr.responseXML;

//XML  DOM树

header('Content-Type: application/json');

echo '[{"name":"Tom","age":20},{},{}]';

JSON.parse( xhr.responseText )

 

 

0 0
原创粉丝点击