资源描述:
《ajax发送json字符窜,php解析json并返回的例子》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、前台AJAX发送JSON字符窜,PHP解析JSON并返回1.前台js用extjs框架发送//svaluearryAddAll是json对象要先转为字符串vartoJsonString=newNParser.JSON();varpostString=toJsonString.write(svaluearryAddAll,false);Ext.Ajax.request({url:'php/excel/test_excel.php',method:'post',success:function(response){a
2、lert(response.responseText);//window_inf(eval('('+response.responseText+')'));},failure:function(response){Ext.Msg.alert('连接失败',response.responseText);},params:{json:postString,cc:'123'}});1.后台php
3、ext/html;charset=utf-8");//header("Content-type:application/vnd.ms-excel");//header("Content-Disposition:filename=test.xls");$jsonString=$_POST['json'];//stripslashes清理反斜杆的,因为前台ajax发送时会自动为双引号单引号加上反斜杆!$json=stripslashes($jsonString);//把json字符串转为数组,true是数组,fal
4、se是转为对象$jsonArray=json_decode($json,true);//echo$jsonArray['layer'];//echo$jsonArray['sum'];echo$jsonArray['layer'];?>Json_encode(),json_decode()用法1.Json_encode(),把数组或者对象转为json格式的字符串$arr=array('a'=>www.zzarea.com,'b'=>"cc",'c'=>3,'d'=>4,'e'=>5);//数组$carray=j
5、son_encode($arr);echo$carray;//结果{"a":www.zzarea.com,"b":"cc","c":3,"d":4,"e":5}json字符串2.Json_decode(),把json字符串转为数组或者对象.其中Json_decode(a,b)有两个参数,第一个是,json字符串,第二个true或者false,当为true是返回一个数组,当为false是返回一个对象.$json1='{"layer":"aabb","properties":{"serial_id":"3"}}';
6、$jsonArray=json_decode($json1,true);//返回数组echo$jsonArray['layer'];//结果是aabb……………………………………………………………………………$json1='{"layer":"aabb","properties":{"serial_id":"3"}}';$jsonArray=json_decode($json1,false);//返回对象echo$jsonArray->layer;//结果是aabbPhp数组,能获取key和value的循环例子
7、1$arr1=array('a'=>1,'b'=>2);foreach($arr1as$key=>$value){echo$key.''.$value.'
';}例子2$a=array("one"=>1,"two"=>2,"three"=>3,"seventeen"=>17);foreach($aas$k=>$v){echo"$a[$k]=>$v".'
';