Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of decode parsing failure of php json

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, Xiaobian will bring you an example analysis of the failure of decode resolution of php json. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

Solution to php json_decode parsing failure: 1. Obtain error code through json_last_error and other functions;2. Eliminate illegal utf8 characters according to utf8 encoding range.

Operating environment: Windows 7 system, PHP 7.1 version, DELL G3 computer

What if php json_decode fails to parse?

php json_decode parsing failure and error handling

Under normal circumstances, get a json content, directly json_decode($content, true) will be converted to array to use, very convenient.

However, if there is a problem with the interface that gives you json content, the json given is not standard or simply wrong, then find a way to find out.

Let's look at json_encode's manual first.

https://www.php.net/manual/en/function.json-last-error.php

Returns NULL on failure

// $json = '{"a":1,"b":2,"c":3,"d":4,"e":5, "name":"Corwien"}'; $json = '{"a":1,"b":2,"c":3,"d":4,"e":5, "name":}'; //Incorrect json format $result = json_decode($json, true); if(!$ result) { //error handle , error handling $ret = json_last_error(); print_r($ret); //Print as: 4, check the error information table, you can see that it is a syntax error } json_last_error error msg Comparison table: 0 = JSON_ERROR_NONE1 = JSON_ERROR_DEPTH2 = JSON_ERROR_STATE_MISMATCH3 = JSON_ERROR_CTRL_CHAR4 = JSON_ERROR_SYNTAX5 = JSON_ERROR_UTF8

How do we know what's wrong?

1. Get the error code

php has a json_last_error function, see

https://www.php.net/manual/en/function.json-last-error.php

It returns an error code telling us what went wrong.

Can't understand the error code? You can use json_last_error_msg, see

https://www.php.net/manual/en/function.json-last-error-msg.php

However, json_last_error_msg is only available in php >= 5.5.0. If the version is low, define one yourself.

2, Low version php json error code incomplete

However, if you look at manual, you will find that many error codes defined by json_last_error are only available in higher versions, and lower versions of php will stop. For example, the JSON_ERROR_UTF8 error code explicitly tells us that there are illegal utf8 characters in the json string, but only in Php >= 5.3.3. Unfortunately, my PHP is 5.3.2.

So, if your json_last_error returns JSON_ERROR_NONE (0), it doesn't mean there are no errors, it's just that the error isn't defined in your earlier version of php. Besides, how can we fail without mistakes...

If it is json format error, then the lower version of php will tell you JSON_ERROR_SYNTAX, so hit JSON_ERROR_NONE the first possibility to illegal utf8 string think.

3. How to deal with illegal utf8 characters in json

According to the coding range of utf8, illegal utf8 characters can be eliminated.

See magp.ie/2011/01/06/remove-non-utf8-characters-from-string-with-php/

//reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ?$ some_string = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'. '|[\x00-\x7F][\x80-\xBF]+'. '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'. '|[\xC2-\xDF]((?! [\x80-\xBF])|[\x80-\xBF]{2,})'. '|[\xE0-\xEF](([\x80-\xBF](?! [\x80-\xBF]))|(?! [\x80-\xBF]{2})|[\x80-\xBF]{3,})/S', '? ', $some_string ); //reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?$ some_string = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'. '|\xED[\xA0-\xBF][\x80-\xBF]/S','? ', $some_string );

Here is the illegal character replaced by?, Change it as needed.

The above is a small series for everyone to share the php json decode analysis failed example analysis, if there is a similar doubt, may wish to refer to the above analysis to understand. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report