Home  >  Article  >  Backend Development  >  How to use JSON data in Python? (code example)

How to use JSON data in Python? (code example)

青灯夜游
青灯夜游Original
2019-01-16 16:41:403342browse

JSON is a JavaScript object notation. It means a script (executable) file consisting of text in a programming language that can be used to store and transfer data. So how to use JSON data in Python? This article will give you a brief introduction to how to use JSON data in Python. I hope it will be helpful to you.

How to use JSON data in Python? (code example)

Python can support json through a built-in package called json; to use this feature, we need to import this json package in the python script.

The following is a simple code example to introduce how to use JSON data in Python.

Example 1:

#导入JSON包
import json 
#键:值 映射
a ={
   "name":"John", 
   "age":31, 
    "Salary":25000
   } 
#通过dumps()函数转换为json
b = json.dumps(a);
#输出
print(b) ;

Output:

How to use JSON data in Python? (code example)

Explanation:

Text in JSON is completed by a quoted string that contains the value in the key-value map. It is similar to a dictionary in Python. JSON exposes an API similar to that used by users of the standard library's marshal and pickle modules, and Python natively supports JSON features.

Example 2:

#导入JSON包
import json 

# JSON支持不同的基元类型
print(json.dumps(['Welcome', "to", "GeeksforGeeks"])) 
print(json.dumps(("Welcome", "to", "GeeksforGeeks"))) 
print(json.dumps("Hi")) 
print(json.dumps(123)) 
print(json.dumps(23.572)) 
print(json.dumps(True)) 
print(json.dumps(False)) 
print(json.dumps(None))

Output:

How to use JSON data in Python? (code example)

Explanation: JSON supports basic types, such as strings and numbers, as well as nested lists, tuples and objects

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to use JSON data in Python? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is Python used for?Next article:What is Python used for?