【问题标题】:Need JSON Dumps to give me string without single quotes需要 JSON 转储给我没有单引号的字符串
【发布时间】:2018-08-31 06:48:45
【问题描述】:

问题有所不同,在于预期输出如何处理 json。很抱歉浪费了您的时间。

我有一个 python 字典对象,我试图以字符串格式返回它,以便另一个函数进行字符串比较。我无法控制其他功能,因此我有责任以请求的格式返回。

现在我有这个 myfunction(params) 返回 (json.dump(dictionary object))

'{"Engineering": {"employees": 3, "employees_with_outside_friends": 2}, "HR": {"employees": 1, "employees_with_outside_friends": 1}, "Business": {"employees": 1, "employees_with_outside_friends": 1}, "Directors": {"employees": 1, "employees_with_outside_friends": 0}}'

但我希望返回看起来像我运行 print(dict) 时打印的常规字符串

{"Engineering": {"employees": 3, "employees_with_outside_friends": 2}, "HR": {"employees": 1, "employees_with_outside_friends": 1}, "Business": {"employees": 1, "employees_with_outside_friends": 1}, "Directors": {"employees": 1, "employees_with_outside_friends": 0}}

示例代码:

def get_actual_output():
    dict_ = dict({"Engineering": {"employees": 3, "employees_with_outside_friends": 2},"HR": {"employees": 1, "employees_with_outside_friends": 1},"Business": {"employees": 1, "employees_with_outside_friends": 1},"Directors": {"employees": 1, "employees_with_outside_friends": 0}})
    return(json.dumps(dict_))

expected_output = '{"Engineering": {"employees": 3, "employees_with_outside_friends": 2},"HR": {"employees": 1, "employees_with_outside_friends": 1},"Business": {"employees": 1, "employees_with_outside_friends": 1},"Directors": {"employees": 1, "employees_with_outside_friends": 0}}'

get_actual_output() == expected_output returns False

添加更多细节:

当我打印上述函数的返回值和预期值时,我得到的是:

>>> actual_output
'{"Business": {"employees": 1, "employees_with_outside_friends": 1}, "Dir
ectors": {"employees": 1, "employees_with_outside_friends": 0}, "Engineer
ing": {"employees": 3, "employees_with_outside_friends": 2}, "HR": {"empl
oyees": 1, "employees_with_outside_friends": 1}}'

>>> test['expected_output']
{'Business': {'employees': 1, 'employees_with_outside_friends': 1},
 'Directors': {'employees': 1, 'employees_with_outside_friends': 0},
 'Engineering': {'employees': 3, 'employees_with_outside_friends': 2},
 'HR': {'employees': 1, 'employees_with_outside_friends': 1}}

【问题讨论】:

  • json.dumps 已经返回,没有单引号。它使用双引号。下一个问题?
  • json.dumps 不返回包含单引号的字符串;单引号是字符串repr 的一部分。
  • 你只是打印错了。请出示minimal reproducible example的代码
  • 字典中有布尔值吗?它们在 python 中看起来像 False,在 json 中看起来像 false
  • 因此,在您添加的新细节中,test['expected_output'] 根本不是字符串。这是一个字典。

标签: python json python-3.x


【解决方案1】:

Json 与 python 字典表示不同,当你打印字典时,python 不使用 json 语法来生成字典的字符串表示,dict 对象有自己的 __str__ 实现,它在调用 str 或执行时调用print(在对象上调用 str)。

str(dict_)产生一个由__str__生成的字符串,不遵循json语法,类似但又不一样,这是两种不同的语法。

另外,json 只允许对字符串使用双引号,而 python 允许双引号和单引号。

对 json 进行简单处理以使其看起来像 dict 的 python repr(如删除/替换引号..)是一个坏主意,在处理复杂对象表示甚至浮点数时会遇到问题。

如果你想要一个看起来像通过打印 dict 生成的字符串的字符串,请使用 str(dict_)。

【讨论】:

    【解决方案2】:

    您的 get_actual_output() == expected_output 示例不匹配,只是因为您在某些键之前缺少空格。例如:...s": 2},"HR": {"em...,其中实际输出为...s": 2}, "HR": {"em...。您无法从 json.dumps 中获得您想要的格式,因为您在某些键之前有空格,而在其他键之前没有空格。

    您可以使用json.dumps(..., separators=(',', ': ')) 删除所有空格,但这可能只会使情况更加混乱。

    理想情况下,您应该解析字符串响应并进行比较。这样,您还可以避免元素顺序无法保证的问题。

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2019-01-22
      • 2022-06-11
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多