Home > php教程 > php手册 > Sencha touch 与PHP 怎么进行json 数据解析

Sencha touch 与PHP 怎么进行json 数据解析

WBOY
Release: 2016-06-06 19:58:02
Original
1099 people have browsed it

ST 可以把数据包装成json式发送到php端 cartItemsStore = Ext.getStore('cartitemsconvert');var allData = [];cartItemsStore.each(function(records){allData.push(records.getData());});var dataToBeSentToServer = Ext.JSON.encode(allData);console.lo

ST 可以把数据包装成json格式发送到php端

cartItemsStore = Ext.getStore('cartitemsconvert');
				var allData = [];
				cartItemsStore.each(function(records){
					allData.push(records.getData());
				});
				var dataToBeSentToServer = Ext.JSON.encode(allData);
				console.log(dataToBeSentToServer);
Copy after login

然后用jsonp或是ajax将数据传输到PHP端
Ext.data.JsonP.request({
            		url: 'http://londit-50c03e41547f8.com.1130.url-test.com/dingcan/php/save-orders.php',
            		callbackKey: 'callback',
            		params: {
		                sendData:dataToBeSentToServer
            		},
            		timeout:2000,
            		success: function(result, request) {
	                	console.log('提交数据成功');
	            	}
        		});
Copy after login

然后PHP怎么获得这些json数据呢?红色部分是php的问题。如果不用stripslashes转换。stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。否则数据打印出来是空,还有个解决办法就是:The fix was to turn off magic_quotes in php.ini. If you do this, you will no longer need the call to stripslashes(). 
$sendData = $_REQUEST['sendData'];
<span>$jsondata = stripslashes($sendData);</span>
//$sendData = '[{"id":"1","food":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","food":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';
$arrayData = json_decode($jsondata);
foreach($arrayData as $obj){
	echo "id:".$obj->food."<br>";
}
Copy after login

另外附上PHP两种解析json数据的方法:

在PHP代码中处理JSON 格式的字符串的两种方法:唯一不同已经highlight出来了

方法一:

$json ='[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';
<span>$students = json_decode($json);//得到的是 object</span>
foreach($students as $obj){
echo "姓名:".$obj->name."年龄:".$obj->age." 专业:".$obj->subject."<br>";
}
Copy after login


方法二:

$json ='[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';
<span>$students = json_decode($json, </span><span>true</span><span>);//得到的是 array</span>
for($i=0;$i<count echo></count>";
}
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