php使用json_decode解析json返回NULL
问一下各位高手,为啥json_decode解析$_POST['mypostdata']字符串会是null,但是把$_POST['mypostdata']复制到php文件中可以正常解析,怎么办?请大家指点一下!代码如下:
header('content-type:text/html; charset=utf-8');
$member_info=$_POST['mypostdata'].trim();
$encode = mb_detect_encoding($member_info, array("ASCII","UTF-8","GB2312","GBK","BIG5"));
echo $member_info.'
';
echo $encode.'
';
$arr = json_decode($member_info,true);
if ($arr == null) echo 'arr是null
';
?>
打印输出:
[["90987682","陈好茹","管理"],["309888729","张先生","管理"],["56439871","jack","管理"],["76398723","李洁","e"]]
UTF-8
arr是null
当然问这个问题之前,楼主也是在网上查了很久的资料例如 (php使用json_decode返回NULL):http://www.nginx.cn/337.html 提到的3种解决办法:
1. json字符串必须以双引号包含
$output = str_replace("'", '"', $output);
2. json字符串必须是utf8编码
$output = iconv('gbk', 'utf8', $output);
3.不能有多余的逗号 如:[1,2,]
用正则替换掉,preg_replace('/,\s*([\]}])/m', '$1', $output)
对于1,2,3对比打印的字符串格式应该是没有问题的,上面的编码也打印出来了是UTF-8的,还真的不知道是咋回事!
另外楼主用的php版本PHP Version 5.2.6应该是用不了json_last_error()函数的 参考:http://php.net/manual/zh/function.json-last-error.php
json_last_error — 返回最后发生的错误 (PHP 5 >= 5.3.0, PHP 7)
-------------------分割线:楼主直接接上面红色部分的json字符串复制到$member_info做json解析,而不是通过post参数获取----------------------
$member_info='[["90987682","陈好茹","管理"],["309888729","张先生","管理"],["56439871","jack","管理"],["76398723","李洁","e"]]';
$encode = mb_detect_encoding($member_info, array("ASCII","UTF-8","GB2312","GBK","BIG5"));
echo $member_info.'
';
echo $encode.'
';
$arr = json_decode($member_info,true);
if ($arr == null) {
echo 'arr是null
';
} else {
foreach($arr as $ele_arr) {
echo '名字:'.$ele_arr[1];
echo '
';
}
}
?>
打印输出
[["90987682","陈好茹","管理"],["309888729","张先生","管理"],["56439871","jack","管理"],["76398723","李洁","e"]]
UTF-8
名字:陈好茹
名字:张先生
名字:jack
名字:李洁
再不确定的问一下:以上应该说明json格式是没有问题的? 字符utf-8编码也是没有问题的?
回复讨论(解决方案)
看上去没有问题
你 echo base64_encode($_POST['mypostdata']); 贴出结果,让我分析一下
看上去没有问题
你 echo base64_encode($_POST['mypostdata']); 贴出结果,让我分析一下
一下是base64_encode数据,多谢了!
W1tcIjkwOTg3NjgyXCIsXCLpmYjlpb3ojLlcIixcIueuoeeQhlwiXSxbXCIzMDk4ODg3MjlcIixcIuW8oOWFiOeUn1wiLFwi566h55CGXCJdLFtcIjU2NDM5ODcxXCIsXCJqYWNrXCIsXCLnrqHnkIZcIl0sW1wiNzYzOTg3MjNcIixcIuadjua0gVwiLFwiZVwiXV0
$s = base64_decode("W1tcIjkwOTg3NjgyXCIsXCLpmYjlpb3ojLlcIixcIueuoeeQhlwiXSxbXCIzMDk4ODg3MjlcIixcIuW8oOWFiOeUn1wiLFwi566h55CGXCJdLFtcIjU2NDM5ODcxXCIsXCJqYWNrXCIsXCLnrqHnkIZcIl0sW1wiNzYzOTg3MjNcIixcIuadjua0gVwiLFwiZVwiXV0");echo $s;
[[\"90987682\",\"陈好茹\",\"管理\"],[\"309888729\",\"张先生\",\"管理\"],[\"56439871\",\"jack\",\"管理\"],[\"76398723\",\"李洁\",\"e\"]]
可以看到双引号被转义了,显然你的 magic_quotes_gpc 开关是打开的(到 php5.4 这个开关就无效了)
由于使用了自动转义,所以不是直接入库的话,需要去转义
if(get_magic_quotes_gpc()) { if(isset($_GET)) $_GET = unTurn($_GET); if(isset($_POST)) $_POST = unTurn($_POST);}//去转义function unTurn($val) { if(is_array($val)) { $val = array_map('unTurn', $val); }else { $val = stripslashes($val); } return $val;}
$s = base64_decode("W1tcIjkwOTg3NjgyXCIsXCLpmYjlpb3ojLlcIixcIueuoeeQhlwiXSxbXCIzMDk4ODg3MjlcIixcIuW8oOWFiOeUn1wiLFwi566h55CGXCJdLFtcIjU2NDM5ODcxXCIsXCJqYWNrXCIsXCLnrqHnkIZcIl0sW1wiNzYzOTg3MjNcIixcIuadjua0gVwiLFwiZVwiXV0");$s = stripcslashes($s);print_r(json_decode($s, true));
Array( [0] => Array ( [0] => 90987682 [1] => 陈好茹 [2] => 管理 ) [1] => Array ( [0] => 309888729 [1] => 张先生 [2] => 管理 ) [2] => Array ( [0] => 56439871 [1] => jack [2] => 管理 ) [3] => Array ( [0] => 76398723 [1] => 李洁 [2] => e ))
好吧,果然版主,思路清晰,问题解决直接了当,毫不拖泥带水,多谢了。 本来可以结贴了,不过本着能慧更多人的目的,请版主分享一下解决这类问题的心得呗!
学习了。
很好用,好理解

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.
