PHP处理Json字符串解码返回NULL的解决方法_php技巧
本文实例讲述了PHP处理Json字符串解码返回NULL的解决方法。分享给大家供大家参考之用。具体方法如下:
一般来说,php对json字符串解码使用json_decode()函数,第一个参数传字符串,第二个参数若为true,返回array;若为false,返回object。如果返回NULL,说明报错,输出json_last_error(),得到的整数值对应错误提示。如下图所示:
json_last_error()比较常见的是整数4, 是json字符串在json_decode之前已不完整,所以语法错误。
那么一定是客户端提交的个别字符影响了json的格式,可以使用JS进行过滤,可以解决一般问题,主要过滤回车,空格,html标签。
实现代码如下:
/* * 过滤函数 */ function htmlEncode(str) { str = str.replace(/\s+/ig, ''); str = str.replace(/&/g, ''); str = str.replace(/</g, ''); str = str.replace(/>/g, ''); str = str.replace(/(?:t| |v|r)*n/g, '<br />'); str = str.replace(/t/g, ' '); str = str.replace(/x22/g, '"'); str = str.replace(/x27/g, '''); str = str.replace(/"/g, ""); return str; }
以上情况针对的是,你必须提交json字符串数据到服务端处理,只能在客户端进行过滤。
其它的json_decode($str)返回NULL的一些原因:
1.$str只能UTF-8编码
2.元素最后不能有逗号(与php的array不同)
3.元素不能使用单引号
4.元素值中间不能有空格和 ,必须替换
如果大家遇到了上述情况,可以按照以上方法处理一下。希望本文所述对大家的PHP程序设计有所帮助。

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











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

In IntelliJ...

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Tomcat starts Servlet error check When troubleshooting. When deploying Servlet application, Tomcat failed to start and reported java.lang.IllegalStateException:...

In processing next-auth generated JWT...

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.
