Python中的 json.dumps()
JSON 的完整形式是 JavaScript Object Notation。这意味着由编程语言中的文本组成的脚本(可执行)文件用于存储和传输数据。 Python通过一个名为 json 的内置包支持 JSON。要使用此功能,我们在Python脚本中导入 json 包。 JSON中的文本是通过quoted-string完成的,该字符串包含{}内键值映射中的值。它类似于Python中的字典。
注意:有关详细信息,请参阅使用Python读取、写入和解析 JSON
json.dumps()
json.dumps()函数将Python对象转换为 json字符串。
Syntax:
json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
Parameters:
obj:Serialize obj as a JSON formatted stream
skipkeys:If skipkeys is true (default: False), then dict keys that are not of a basic type (str, int, float, bool, None) will be skipped instead of raising a TypeError.
ensure_ascii:If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is false, these characters will be output as-is.
check_circular :If check_circular is false (default: True), then the circular reference check for container types will be skipped and a circular reference will result in an OverflowError (or worse).
allow_nan :If allow_nan is false (default: True), then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification. If allow_nan is true, their JavaScript equivalents (NaN, Infinity, -Infinity) will be used.
indent:If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, negative, or “” will only insert newlines. None (the default) selects the most compact representation. Using a positive integer indent indents that many spaces per level. If indent is a string (such as “\t”), that string is used to indent each level.
separators :If specified, separators should be an (item_separator, key_separator) tuple. The default is (‘, ‘, ‘: ‘) if indent is None and (‘, ‘, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘, ‘, ‘:’) to eliminate whitespace.
default:If specified, default should be a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError. If not specified, TypeError is raised.
sort_keys :If sort_keys is true (default: False), then the output of dictionaries will be sorted by key.
示例 #1:将Python字典传递给 json.dumps()函数将返回一个字符串。
Python3
import json
# Creating a dictionary
Dictionary ={1:'Welcome', 2:'to',
3:'Geeks', 4:'for',
5:'Geeks'}
# Converts input dictionary into
# string and stores it in json_string
json_string = json.dumps(Dictionary)
print('Equivalent json string of input dictionary:',
json_string)
print(" ")
# Checking type of object
# returned by json.dumps
print(type(json_string))
Python3
import json
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
3:'Geeks', 4:'for',
5:'Geeks'}
# Our dictionary contains tuple
# as key, so it is automatically
# skipped If we have not set
# skipkeys = True then the code
# throws the error
json_string = json.dumps(Dictionary,
skipkeys = True)
print('Equivalent json string of dictionary:',
json_string)
Python3
import json
# We are adding nan values
# (out of range float values)
# in dictionary
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
3:'Geeks', 4:'for',
5:'Geeks', 6:float('nan')}
# If we hadn't set allow_nan to
# true we would have got
# ValueError: Out of range float
# values are not JSON compliant
json_string = json.dumps(Dictionary,
skipkeys = True,
allow_nan = True)
print('Equivalent json string of dictionary:',
json_string)
Python3
import json
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
3:'Geeks', 4:'for',
5:'Geeks', 6:float('nan')}
# Indentation can be used
# for pretty-printing
json_string = json.dumps(Dictionary,
skipkeys = True,
allow_nan = True,
indent = 6)
print('Equivalent json string of dictionary:',
json_string)
Python3
import json
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
3:'Geeks', 4:'for',
5:'Geeks', 6:float('nan')}
# If specified, separators should be
# an (item_separator, key_separator)tuple
# Items are separated by '.' and key,
# values are separated by '='
json_string = json.dumps(Dictionary,
skipkeys = True,
allow_nan = True,
indent = 6,
separators =(". ", " = "))
print('Equivalent json string of dictionary:',
json_string)
Python3
import json
Dictionary ={'c':'Welcome', 'b':'to',
'a':'Geeks'}
json_string = json.dumps(Dictionary,
indent = 6,
separators =(". ", " = "),
sort_keys = True)
print('Equivalent json string of dictionary:',
json_string)
输出
Equivalent json string of dictionary: {“1”: “Welcome”, “2”: “to”, “3”: “Geeks”, “4”: “for”, “5”: “Geeks”}
示例 #2:通过将 skipkeys 设置为 True(默认值:False),我们会自动跳过非基本类型的键。
Python3
import json
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
3:'Geeks', 4:'for',
5:'Geeks'}
# Our dictionary contains tuple
# as key, so it is automatically
# skipped If we have not set
# skipkeys = True then the code
# throws the error
json_string = json.dumps(Dictionary,
skipkeys = True)
print('Equivalent json string of dictionary:',
json_string)
输出
Equivalent json string of dictionary: {“2”: “to”, “3”: “Geeks”, “4”: “for”, “5”: “Geeks”}
示例#3:
Python3
import json
# We are adding nan values
# (out of range float values)
# in dictionary
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
3:'Geeks', 4:'for',
5:'Geeks', 6:float('nan')}
# If we hadn't set allow_nan to
# true we would have got
# ValueError: Out of range float
# values are not JSON compliant
json_string = json.dumps(Dictionary,
skipkeys = True,
allow_nan = True)
print('Equivalent json string of dictionary:',
json_string)
输出 :
Equivalent json string of dictionary: {“2”: “to”, “3”: “Geeks”, “4”: “for”, “5”: “Geeks”, “6”: NaN}
示例 #4:
Python3
import json
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
3:'Geeks', 4:'for',
5:'Geeks', 6:float('nan')}
# Indentation can be used
# for pretty-printing
json_string = json.dumps(Dictionary,
skipkeys = True,
allow_nan = True,
indent = 6)
print('Equivalent json string of dictionary:',
json_string)
输出:
Equivalent json string of dictionary: {
"2": "to",
"3": "Geeks",
"4": "for",
"5": "Geeks",
"6": NaN
}
示例 #5:
Python3
import json
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
3:'Geeks', 4:'for',
5:'Geeks', 6:float('nan')}
# If specified, separators should be
# an (item_separator, key_separator)tuple
# Items are separated by '.' and key,
# values are separated by '='
json_string = json.dumps(Dictionary,
skipkeys = True,
allow_nan = True,
indent = 6,
separators =(". ", " = "))
print('Equivalent json string of dictionary:',
json_string)
输出:
Equivalent json string of dictionary: {
"2" = "to".
"3" = "Geeks".
"4" = "for".
"5" = "Geeks".
"6" = NaN
}
示例 #6:
Python3
import json
Dictionary ={'c':'Welcome', 'b':'to',
'a':'Geeks'}
json_string = json.dumps(Dictionary,
indent = 6,
separators =(". ", " = "),
sort_keys = True)
print('Equivalent json string of dictionary:',
json_string)
输出:
Equivalent json string of dictionary: {
"a" = "Geeks".
"b" = "to".
"c" = "Welcome"
}