How does php determine whether the data is in json format?

Source: Internet
Author: User
Next I will summarize several articles for my friends on how to determine whether php is a json data. here I have sorted out some articles to help you know how to use php to determine json data, I hope the example will help you. remember that json_encode returns characters... next I will summarize several articles for my friends on how to determine whether php is a json data. here I have sorted out some articles to help you know how to use php to determine json data, I hope the example will help you.

Remember that json_encode returns a string, while json_decode returns an object.

The code is as follows:

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

Determine that the data is legal json data. The PHP version is later than 5.3 and 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 an error during the data encoding/decoding process.

Note: The json codec operation string must be UTF8.

The code is as follows:

 

If it is not json, false is returned.


Article URL:

Reprint ^ at will, but please attach the tutorial address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.