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

How does php determine whether the data is json?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

I hope you can read it carefully and be able to achieve something!

Php method to determine whether the data is json: 1, create a PHP example file; 2, through the "function analyJson ($json_str) {...}" method to determine whether the data is json.

This article operating environment: windows7 system, PHP7.4 version, DELL G3 computer

How does php tell if the data is json?

The method of judging whether it is in json format by php

Http://www.poluoluo.com/jzxy/201403/265005.html

How does php tell if the data is json?

The first thing to remember is that json_encode returns a string and json_decode returns an object

Determine that the data is not in JSON format:

The code is as follows:

Function is_not_json ($str) {return is_null (json_decode ($str));}

Determine that the data is legitimate json data: (PHP version is greater than 5.3)

The code is as follows:

Function is_json ($string) {json_decode ($string); return (json_last_error () = = JSON_ERROR_NONE);}

The json_last_error () function returns errors that occur during data encoding and decoding.

Note: the string operated by json codec must be UTF8.

Examples

The code is as follows:

Return false if it is not json

This is the end of the content of "how php determines whether the data is json". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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