Home > Backend Development > PHP Tutorial > 请问为什么,json_decode转uft-8字符串,结果为null?

请问为什么,json_decode转uft-8字符串,结果为null?

WBOY
Release: 2016-06-23 13:58:09
Original
1086 people have browsed it



<html> <html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"/></head><?php$str=isset($_POST["text"]) ? filter_input(INPUT_POST,"text",FILTER_SANITIZE_SPECIAL_CHARS) : '';print "$str=".$str."\n";if (strlen($str)>0){//$str=mb_convert_encoding($str,"GBK","UTF-8");$jo=json_decode($str);  //中文的,转出来是nullprint_r("\n++++\n".$jo."\n====\n"); $msg_id=$jo->msg_id; //这里就会报错了。}echo ' <form method="post" action="t1.php">';echo ' <textarea name="text" rows="2" cols="80">{"name":"test","id":0,"txt":"中文"}</textarea><br>';echo ' <input type="submit"></form>';?>
Copy after login


回复讨论(解决方案)

json 格式不对吧 ,你把j要转换的json字符串echo 出来看看 是否符合标准json格式

改简单一点:

<html> <html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"/></head><?php$str="{\"txt\":\"中文\"}</textarea><br>";print "$str=".$str."\n";if (strlen($str)>0){$jo=json_decode($str);  //中文的,转出来是nullprint_r("\n++++\n".$jo."\n====\n"); $msg_id=$jo->txt;}
Copy after login

我把这个t1.php文件保存成utf-8编码了。

但还是不行。

加上后面的
 已经不是json格式了。

print_r("\n++++\n".$jo."\n====\n"); 
这里是什么?

echo $_POST["text"];
看看是什么,多半是引号被转义了

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template