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