Python Parse JSON –转储,加载
在本教程中,我们将讨论Python JSON。
如何使用python编码和解码JSON数据。
在之前的教程中,我们讨论了有关Python raw_input的问题。
Python JSON
在开始使用Python的json模块之前,我们将首先讨论JSON数据。
JSON的缩写是JavaScript Object Notation。
根据Wikipedia的说法,JSON是一种开放标准的文件格式,它使用人类可读的文本来传输由属性值对和数组数据类型(或者任何其他可序列化的值)组成的数据对象。
JSON是一种非常常见的数据格式,用于异步浏览器/服务器通信。
JSON的语法规则如下:
- 数据只是一个名称值对
- 数据/对象/数组用逗号分隔
- 花括号支撑对象
- 方阵
您可能会在这里看到一些JSON数据示例。
Python JSON转储
在本节中,我们将学习如何将python数据转换为JSON数据。
任务很简单。
首先导入json模块。
然后使用json.dumps()函数解码json数据。
以下是python json转储函数的简单示例。
import json # initialize different data str_data = 'normal string' int_data = 1 float_data = 1.50 list_data = [str_data, int_data, float_data] nested_list = [int_data, float_data, list_data] dictionary = { 'int': int_data, 'str': str_data, 'float': float_data, 'list': list_data, 'nested list': nested_list } # convert them to JSON data and then print it print('String :', json.dumps(str_data)) print('Integer :', json.dumps(int_data)) print('Float :', json.dumps(float_data)) print('List :', json.dumps(list_data)) print('Nested List :', json.dumps(nested_list, indent=2)) print('Dictionary :', json.dumps(dictionary, indent=2)) # the json data will be indented
您将得到这样的输出。
String : "normal string" Integer : 1 Float : 1.5 List : ["normal string", 1, 1.5] Nested List : [ 1, 1.5, [ "normal string", 1, 1.5 ] ] Dictionary : { "int": 1, "str": "normal string", "float": 1.5, "list": [ "normal string", 1, 1.5 ], "nested list": [ 1, 1.5, [ "normal string", 1, 1.5 ] ] }
Python JSON漂亮打印
如您在上面的示例中所见,对于json精美打印,我们必须将一个另外的变量"缩进"传递给json转储函数。
例如json.dumps(nested_list,indent = 2)
。
Python解析json – python json加载
您可以轻松地将JSON数据解析为Python对象。
通过使用json.loads()函数,您可以简单地将JSON数据转换为Python数据。
因此,请参阅以下python parse json示例代码以了解python json加载功能。
import json # initialize different JSON data arrayJson = '[1, 1.5, ["normal string", 1, 1.5]]' objectJson = '{"a":1, "b":1.5 , "c":["normal string", 1, 1.5]}' # convert them to Python Data list_data = json.loads(arrayJson) dictionary = json.loads(objectJson) print('arrayJson to list_data :\n', list_data) print('\nAccessing the list data :') print('list_data[2:] =', list_data[2:]) print('list_data[:1] =', list_data[:1]) print('\nobjectJson to dictionary :\n', dictionary) print('\nAccessing the dictionary :') print('dictionary[\'a\'] =', dictionary['a']) print('dictionary[\'c\'] =', dictionary['c'])
以下是python parse json示例程序的输出。
arrayJson to list_data : [1, 1.5, ['normal string', 1, 1.5]] Accessing the list data : list_data[2:] = [['normal string', 1, 1.5]] list_data[:1] = [1] objectJson to dictionary : {'a': 1, 'b': 1.5, 'c': ['normal string', 1, 1.5]} Accessing the dictionary : dictionary['a'] = 1 dictionary['c'] = ['normal string', 1, 1.5]
Python对象到JSON数据转换
在前两节中,您可能已经注意到Python列表被转换为JSONArray数据,而Python字典成为JSONObject。
下表显示了默认情况下哪个Python对象转换为JSON对象。
Python | JSON |
---|---|
dict | object |
list, tuple | array |
str | string |
int, float, int- & float-derived Enums | number |
True | true |
False | false |
None | null |
另外,如果您转换JSONArray,则会获得Python列表。
因此,对此也有一些规则。
因此,下表显示了转换为Python数据的JSON数据的类型。
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |