= json_encode(= json_encode(= array('east'=>'tiger','north'=>'wolf','duck','south'=>'monkey'); { = "beijing"; = 170; { ; 27 } 28 } 29 $tom = new Person(); json_encode($tom);//{"addr":"beijing","height":170} 1.json
json_encode(数组/对象)------------>生成json信息,
json_decode(json信息); 反编码json信息
对json字符串信息进行反编码,变为当前语言可以识别的信息。
2. javascript接收处理json信息通过eval()把接收的json字符串变成真实的对象信息
代码如下:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ""> 2 <html> 3 <head> 4 <title>新建网页</title> 5 ="text/html; charset=utf-8" /> 6 ="" /> 7 ="" /> 8 9 <script type="text/javascript"> { xhr = new XMLHttpRequest(); 14 xhr.onreadystatechange = function(){ 15 if(xhr.readyState==4){ console.log(eval("var info="+xhr.responseText); str = "地址:"+info.addr+";温度:"+info.temp+";风向:"+info.wind; 23 document.getElementById('result').innerHTML = str; 24 } 25 } 26 xhr.open('get','./03.php'); 27 xhr.send(null); 28 } 29 window.onload = function(){ 30 showweather(); 31 } </script> 41 42 <style type="text/css"> 43 </style> 44 </head> 45 <body> 46 <h2>获得天气预报接口信息</h2> 47 > 48 </body> 49 </html>