热搜:NVER node 开发 php

php接受json格式的数据如何解析

2024-09-20 10:50:01
php接受json格式的数据如何解析

PHP JSON Jquery

$(document).ready(function(){        var jsonText ="{'user':[{'username':'zhangsan','password':'123'},{'username':'xx','password':'456'}]}";	$("a").click(function(){			$.post('a.php',{data:jsonText},function(data){			alert(data);		});	});});


php文件中要如何解析接收到的data,求大神指教???


回复讨论(解决方案)

用json_decode将json字符串转化为数组

$arr = json_decode($_POST['data'],true);print_r($arr);


Array ( [user] => Array ( [0] => Array ( [username] => zhangsan [password] => 123 ) [1] => Array ( [username] => xx [password] => 456 ) ) )

#1楼正解
用json_decode POST过来的参数就可以了

<?php$arr = json_decode($_POST['data'],true);print_r($arr);?>

返回值在前端接收的时候为空,alert(data);什么也没有,why??

<?php$arr = json_decode($_POST['data'],true);print_r($arr);?>

返回值在前端接收的时候为空,alert(data);什么也没有,why??
你直接打印出接收到的$_POST['data']就知道什么原因了。

<?php$arr = json_decode($_POST['data'],true);print_r($arr);?>

返回值在前端接收的时候为空,alert(data);什么也没有,why??

那你直接输出$_POST['data'],瞧瞧


<?php$arr = json_decode($_POST['data'],true);print_r($arr);?>

返回值在前端接收的时候为空,alert(data);什么也没有,why??
你直接打印出接收到的$_POST['data']就知道什么原因了。

print_r($_POST['data']);是有值的,json_decode后就没有了。。。



<?php$arr = json_decode($_POST['data'],true);print_r($arr);?>

返回值在前端接收的时候为空,alert(data);什么也没有,why??
你直接打印出接收到的$_POST['data']就知道什么原因了。

print_r($_POST['data']);是有值的,json_decode后就没有了。。。
有值又不代表json就能解析,贴出来看看

print_r($_POST['data']);前端接收之后,弹出如下图



是不是json格式不正确啊???

print_r($_POST['data']);前端接收之后,弹出如下图



是不是json格式不正确啊???



print_r($_POST['data']);前端接收之后,弹出如下图



是不是json格式不正确啊???





$(document).ready(function(){        var jsonText ="{'user':[{'username':'zhangsan','password':'123'},{'username':'xx','password':'456'}]}";	$("a").click(function(){			$.post('a.php',{data:jsonText},function(data){			alert(data);		});	});});


json格式不是这样的吗?那json格式要如何来写呢?

把前端传递的json字符串改为

var jsonText ="{\"user\":[{\"username\":\"zhangsan\",\"password\":\"123\"},{\"username\":\"xx\",\"password\":\"456\"}]}";

或者改为

var jsonText ='{"user":[{"username":"zhangsan","password":"123"},{"username":"xx","password":"456"}]}';


另外你最两个大括号之间的逗号是全角的吧,改成半角



print_r($_POST['data']);前端接收之后,弹出如下图



是不是json格式不正确啊???





$(document).ready(function(){        var jsonText ="{'user':[{'username':'zhangsan','password':'123'},{'username':'xx','password':'456'}]}";	$("a").click(function(){			$.post('a.php',{data:jsonText},function(data){			alert(data);		});	});});


json格式不是这样的吗?那json格式要如何来写呢?


var jsonText ={"user":[{"username":"zhangsan","password":"123"},{"username":"xx","password":"456"}]};


试试可以不



print_r($_POST['data']);前端接收之后,弹出如下图



是不是json格式不正确啊???





$(document).ready(function(){        var jsonText ="{'user':[{'username':'zhangsan','password':'123'},{'username':'xx','password':'456'}]}";	$("a").click(function(){			$.post('a.php',{data:jsonText},function(data){			alert(data);		});	});});


json格式不是这样的吗?那json格式要如何来写呢?

var jsonText = JSON.stringify({user:[{username:'zhangsan',password:'123'},{username:'xx',password:'456'}]});

如果你发现不支持JSON的浏览器,你可以使用jquery的json插件将js的对象数据转换成json标准格式。

楼主最后怎么解决的 方法分享一下呗

楼主最后怎么解决的 方法分享一下呗

就是按照大家说的把json格式修改了一下,就OK啦,真的是我的json格式写错了。
楼上说的json是正确的
非常感谢大家!