Python's method instance for JSON string judgments

Source: Internet
Author: User
This article mainly introduces the use of Python to determine whether the variable is a JSON-formatted string of relevant data, the text gives a detailed sample code for everyone to refer to the study, the need for friends below to see it together.

JSON introduction

Full name JavaScript Object Notation, is a lightweight data interchange format. The most extensive application of JSON is the data format for the communication of Web servers and clients in Ajax. It is also often used in HTTP requests, so it is natural to learn a variety of JSON.

This article mainly introduces the use of Python to determine whether the variable is a JSON format string, for everyone's daily learning work has a certain reference value, the following words do not say, directly to see the code bar.

The sample code is as follows


#-*-Coding=utf-8-*-import jsondef Check_json_format (raw_msg): "" "is used to determine whether a string conforms to the JSON format:p Aram self:: return:" "" If Isin Stance (Raw_msg, str):  # First determine if the variable is a string  try:   json.loads (raw_msg, encoding= ' utf-8 ')  except ValueError:   return False  return True else:  return falseif name = = "Main": Print Check_json_format ("" "{" a ": 1}" "") Print C Heck_json_format ("" "{' A ': 1}" ") print Check_json_format ({' A ': 1}) print Check_json_format (100)

The first is to determine if the variable is a string, otherwise an error occurs if the input is int or this other type.

The output of the above program is:


Truefalsefalsefalse

Summarize

"Recommended"

1. Python Free video tutorial

2. Python Learning Manual

3. Python Object-oriented video tutorial

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.