Python-JSON模块

时间:2020-02-23 14:42:52  来源:igfitidea点击:

在本教程中,我们将学习在Python中使用JSON。

什么是JSON?

JSON或者JavaScript Object Notation是一种轻量级格式,用于存储和交换数据。

示例:

{
  "username": "theitroadtheitroad",
  "score": 9.1,
  "isOnline": false
}

json模块

要在Python中使用JSON,我们必须导入json模块。

import json

json.dumps()方法

要将Python对象转换为JSON,我们使用json模块中的json.dumps()方法。

在下面的Python程序中,我们将Python字典转换为JSON字符串。

# import module
import json

# dict
userData = {
  "username": "theitroadtheitroad",
  "score": 9.1,
  "isOnline": False
}

# convert to JSON
userData_JSON = json.dumps(userData)

# output
print("Type of userData:", type(userData))
print("Type of userData_JSON:", type(userData_JSON))

print("userData:", userData)
print("userData_JSON:", userData_JSON)

上面的代码将为我们提供以下输出。

Type of userData: <class 'dict'>
Type of userData_JSON: <class 'str'>
userData: {'username': 'theitroadtheitroad', 'score': 9.1, 'isOnline': False}
userData_JSON: {"username": "theitroadtheitroad", "score": 9.1, "isOnline": false}

为了使JSON字符串更具可读性,我们可以将indent参数传递给json.dumps()方法。

在下面的Python程序中,我们将使用json.dumps()方法将Python对象覆盖为JSON字符串,并传递indent = 2以使字符串更具可读性。

# import module
import json

# dict
userData = {
  "username": "theitroadtheitroad",
  "score": 9.1,
  "isOnline": False
}

# convert to JSON
userData_JSON = json.dumps(userData, indent=2)

# output
print("Type of userData:", type(userData))
print("Type of userData_JSON:", type(userData_JSON))

print("userData:", userData)
print("userData_JSON:", userData_JSON)

我们将获得以下输出。

Type of userData: <class 'dict'>
Type of userData_JSON: <class 'str'>
userData: {'username': 'theitroadtheitroad', 'score': 9.1, 'isOnline': False}
userData_JSON: {
  "username": "theitroadtheitroad",
  "score": 9.1,
  "isOnline": false
}

Python对象转换为JSON

我们可以使用json.dumps()方法将以下Python对象转换为JSON。

Python对象JSON
Truetrue
Falsefalse
intNumber
floatNumber
strString
列表数组
元组数组
dict对象
Nonenull

在下面的Python程序中,我们将Python对象覆盖为JSON字符串。

# import module
import json

# data
data = {
    "int": 1,
    "float": 3.14,
    "string": "theitroadtheitroad",
    "list": [1, 2, 3],
    "tuple": (1, 2, 3),
    "dict": { "x": 10, "y": 20 },
    "boolTrue": True,
    "boolFalse": False,
    "noneVal": None
}

# convert to JSON
data_JSON = json.dumps(data)

# output
print("Type of data:", type(data))
print("Type of data_JSON:", type(data_JSON))

print("data:", data)
print("data_JSON:", data_JSON)

上面的Python代码将为我们提供以下输出。

Type of data: <class 'dict'>
Type of data_JSON: <class 'str'>
data: {'int': 1, 'float': 3.14, 'string': 'theitroadtheitroad', 'list': [1, 2, 3], 'tuple': (1, 2, 3), 'dict': {'x': 10, 'y': 20}, 'boolTrue': True, 'boolFalse': False, 'noneVal': None}
data_JSON: {
  "int": 1,
  "float": 3.14,
  "string": "theitroadtheitroad",
  "list": [
    1,
    2,
    3
  ],
  "tuple": [
    1,
    2,
    3
  ],
  "dict": {
    "x": 10,
    "y": 20
  },
  "boolTrue": true,
  "boolFalse": false,
  "noneVal": null
}

json.loads()方法

我们使用json.loads()方法将JSON字符串转换为Python对象。

在以下Python程序中,我们将JSON字符串转换为Python对象。

# import module
import json

# json
data_JSON = '{"username": "theitroadtheitroad", "score": 9.1, "isOnline": false}'

# covert
data = json.loads(data_JSON)

# output
print("Type of data_JSON:", type(data_JSON))
print("Type of data:", type(data))

print("data_JSON:", data_JSON)
print("data:", data)

上面的代码将使用以下输出。

Type of data_JSON: <class 'str'>
Type of data: <class 'dict'>
data_JSON: {"username": "theitroadtheitroad", "score": 9.1, "isOnline": false}
data: {'username': 'theitroadtheitroad', 'score': 9.1, 'isOnline': False}