Table of Contents
Solution to PHP processing Json string decoding returning NULL, jsonnull
php receives the json returned by php, and the parsed value is NULL
Convert PHP array to JSON, the string "Restaurant/Café" is converted to json and displayed as null. How can I display the string correctly
Home Backend Development PHP Tutorial Solution to PHP processing Json string decoding returning NULL, jsonnull_PHP tutorial

Solution to PHP processing Json string decoding returning NULL, jsonnull_PHP tutorial

Jul 13, 2016 am 10:19 AM
json null php Solution

Solution to PHP processing Json string decoding returning NULL, jsonnull

The example in this article describes the solution for PHP to handle Json string decoding and return NULL. Share it with everyone for your reference. The specific method is as follows:

Generally speaking, php uses the json_decode() function to decode json strings. The first parameter is a string. If the second parameter is true, it returns an array; if it is false, it returns an object. If NULL is returned, an error is reported and json_last_error() is output. The obtained integer value corresponds to the error prompt . As shown below:

The most common value of json_last_error() is the integer 4, which means that the json string is incomplete before json_decode, so the syntax is wrong.

Then it must be that individual characters submitted by the client affect the format of json. You can use JS to filter, which can solve general problems. It mainly filters carriage returns, spaces, and html tags.

The implementation code is as follows:

/*
* 过滤函数
*/
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;
}

Copy after login

The above situation is for the fact that you must submit json string data to the server for processing, and can only be filtered on the client.

Some reasons why json_decode($str) returns NULL:

1.$str can only be encoded in UTF-8

2. The element cannot have a comma at the end (different from PHP’s array)

3. Elements cannot use single quotes

4. There cannot be spaces or n in the middle of the element value, and

must be replaced.

If you encounter the above situation, you can handle it according to the above methods. I hope this article will be helpful to everyone’s PHP programming.

php receives the json returned by php, and the parsed value is NULL

b.php must submit the cookie value when fetching json content, otherwise a.php will only return empty content.

Convert PHP array to JSON, the string "Restaurant/Café" is converted to json and displayed as null. How can I display the string correctly

Just put your key in single quotes and try it.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/872372.htmlTechArticleSolution to how PHP handles Json string decoding and returns NULL, jsonnull This example describes how PHP handles Json string decoding and returns Solution to NULL. Share it with everyone for your reference. Tool...
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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Deepseek official website entrance access guide Solve the common problems that cannot be logged in Deepseek official website entrance access guide Solve the common problems that cannot be logged in Feb 19, 2025 pm 04:30 PM

Deepseek is a platform that provides anonymous deep network access. To access its official website, please use the official portal link provided. If you encounter problems during login, it may be due to the following reasons: the browser is outdated, the credentials are incorrect, the connection is blocked, maintenance or the account is disabled. Frequently Asked Questions include: Deepseek's security and legality, and how to connect with the support team.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to solve the problem of busy servers for deepseek How to solve the problem of busy servers for deepseek Mar 12, 2025 pm 01:39 PM

DeepSeek: How to deal with the popular AI that is congested with servers? As a hot AI in 2025, DeepSeek is free and open source and has a performance comparable to the official version of OpenAIo1, which shows its popularity. However, high concurrency also brings the problem of server busyness. This article will analyze the reasons and provide coping strategies. DeepSeek web version entrance: https://www.deepseek.com/DeepSeek server busy reason: High concurrent access: DeepSeek's free and powerful features attract a large number of users to use at the same time, resulting in excessive server load. Cyber ​​Attack: It is reported that DeepSeek has an impact on the US financial industry.

gate.io official login web version gate.io login URL 2025 gate.io official login web version gate.io login URL 2025 Feb 20, 2025 pm 02:09 PM

Gate.io Exchange provides users with an official login portal. Through the official website or mobile app, users can log in to their account. The login steps are easy, including entering the email or mobile phone number used when registering, as well as your password. In order to ensure the security of the account, it is recommended that users change their passwords regularly and properly keep their login information. In addition, the article also provides solutions to common login problems, including inability to log in and password loss.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How to adjust Sesame Open Exchange into Chinese How to adjust Sesame Open Exchange into Chinese Mar 04, 2025 pm 11:51 PM

How to adjust Sesame Open Exchange to Chinese? This tutorial covers detailed steps on computers and Android mobile phones, from preliminary preparation to operational processes, and then to solving common problems, helping you easily switch the Sesame Open Exchange interface to Chinese and quickly get started with the trading platform.

Sesame Open Door Exchange App Official Download Sesame Open Door Exchange Official Download Sesame Open Door Exchange App Official Download Sesame Open Door Exchange Official Download Mar 04, 2025 pm 11:54 PM

The official download steps of the Sesame Open Exchange app cover the Android and iOS system download process, as well as common problems solutions, helping you download safely and quickly and enable convenient transactions of cryptocurrencies.

Sesame Open Door Exchange Web Page Login Latest version gateio official website entrance Sesame Open Door Exchange Web Page Login Latest version gateio official website entrance Mar 04, 2025 pm 11:48 PM

A detailed introduction to the login operation of the Sesame Open Exchange web version, including login steps and password recovery process. It also provides solutions to common problems such as login failure, unable to open the page, and unable to receive verification codes to help you log in to the platform smoothly.

See all articles