Home > php教程 > php手册 > body text

PHP JSON返回null或空字符解决办法

WBOY
Release: 2016-05-26 08:20:31
Original
1762 people have browsed it

json只能使用utf-8编码来进行操作,否则就会返回null或空字符串了,下面提供一个非常简单的办法供大家参考.

原因分析:其它的json_decode($str)返回NULL的一些原因:

1.$str只能UTF-8编码

2.元素最后不能有逗号(与php的array不同)

3.元素不能使用单引号

4.元素值中间不能有空格和\n,必须替换

在用PHP JSON处理符合标准的GBK编码内容时会返回null,将内容从GBK转换为UTF8再处理正常,代码如下:

$jsonp = mb_convert_encoding($jsonp, "gbk", "utf-8"); 
//或 
$jsonp = iconv('gbk','utf-8',$jsonp)
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