📜  Python中的 json.dumps()

📅  最后修改于: 2022-05-13 01:54:41.349000             🧑  作者: Mango

Python中的 json.dumps()

JSON 的完整形式是 JavaScript Object Notation。这意味着由编程语言中的文本组成的脚本(可执行)文件用于存储和传输数据。 Python通过一个名为 json 的内置包支持 JSON。要使用此功能,我们在Python脚本中导入 json 包。 JSON中的文本是通过quoted-string完成的,该字符串包含{}内键值映射中的值。它类似于Python中的字典。
注意:有关详细信息,请参阅使用Python读取、写入和解析 JSON

json.dumps()

json.dumps()函数将Python对象转换为 json字符串。

示例 #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)


输出

示例 #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)

输出

示例#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)

输出 :

示例 #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"
}