search
Homephp教程php手册PHP处理Json字符串解码返回NULL的解决方法,jsonnull

PHP处理Json字符串解码返回NULL的解决方法,jsonnull

本文实例讲述了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(/(&#63;:t| |v|r)*n/g, '<br />');
  str = str.replace(/t/g, '    ');
  str = str.replace(/x22/g, '"');
  str = str.replace(/x27/g, '&#39;');
  str = str.replace(/"/g, "");
  return str;
}

以上情况针对的是,你必须提交json字符串数据到服务端处理,只能在客户端进行过滤。

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

1.$str只能UTF-8编码

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

3.元素不能使用单引号

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

如果大家遇到了上述情况,可以按照以上方法处理一下。希望本文所述对大家的PHP程序设计有所帮助。

php接收php返回的json,解析后值为NULL

b.php 必须在取json内容时提交cookie值,否则a.php 只会返回空的内容。
 

PHP array 转换成JSON ,字符串"Restaurant/Café"转成json后显示为null要怎才可以正确显示字符串

用单引号把你的键引起来 就可以了试下。

$arr = array ('Amenities' =>array ('Restaurant/Café' => array('Classroom' => 'Classroom')));
echo json_encode($arr) ;
 

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.