简体   繁体   English

不正确的 python 字典到 JSON 转换

[英]Incorrect python dictionary to JSON conversion

I'm trying to convert a python dictionary to the target JSON object below.我正在尝试将 python 字典转换为下面的target JSON 对象。 I figured I'd use json.dumps() (as per this thread ) but the result is not the same nevertheless.我想我会使用json.dumps() (根据这个线程)但结果不一样。 The target has some unconvential spacing in it, but I'm not allowed to change it or edit them out. target有一些非常规的间距,但我不允许更改或编辑它们。

Any idea how to approach this?知道如何解决这个问题吗?

import json

dict= {"token":{"name":"John Doe","code":"123456789"}}
target = '{ "token":{ "name":"John Doe", "code":"123456789" } }'

print(json.dumps(dict))
print(json.loads(json.dumps(dict)))
print(target)

>>>{"token": {"name": "John Doe", "code": "123456789"}}
>>>{'token': {'name': 'John Doe', 'code': '123456789'}}
>>>{ "token":{ "name":"John Doe", "code":"123456789" } }

For additional context, I'm trying to prepare the argument passed through Bambora's payment API .对于其他上下文,我正在尝试准备通过Bambora's payment API传递的参数。 See the cURL example associated to this here .此处查看与此相关的 cURL 示例。

Since you're comparing strings, you'll get a False result if even one space is different between the two strings.由于您正在比较字符串,因此即使两个字符串之间的空格不同,您也会得到False结果。 This can happen even if the two structures are actually the same in terms of their structure and data.即使这两种结构在结构和数据方面实际上相同,也会发生这种情况。 What you really want to do is find a way to remove non-substantive formatting issues from the equation.您真正想要做的是找到一种方法来从等式中删除非实质性格式问题。

Here's how to fix your code to take away the problem of differences in spacing and other non-substantive differences:以下是如何修复您的代码以消除间距差异和其他非实质性差异的问题:

import json

dict= {"token":{"name":"John Doe","code":"123456789"}}
target = json.dumps(json.loads('{ "token":{ "name":"John Doe", "code":"123456789" } }'))

print(target == json.dumps(dict))

Result:结果:

True

There are some unnecessary whitespaces in your target JSON.目标 JSON 中有一些不必要的空格。

target = '{ "token":{ "name":"John Doe", "code":"123456789" } }'

You can use the separators argument to get a space after the comma separators.您可以使用separators参数在逗号分隔符后获得一个空格。

json.dumps(dict, separators=(', ', ':'))

In order to get the spaces around the curly braces, I am afraid, you will need to use a regular expression based substitution.为了获得花括号周围的空格,恐怕您需要使用基于正则表达式的替换。

json.dumps() returns {"token": {"name": "John Doe", "code": "123456789"}}' json.dumps()返回{"token": {"name": "John Doe", "code": "123456789"}}'

It has no spaces at end of each brackets { and } , but your one string has.每个括号{}末尾都没有空格,但是您的一个字符串有。

This code returns True :此代码返回True

json.dumps(dict) == '{"token": {"name": "John Doe", "code": "123456789"}}'

Let's take a closer look "white spaces".让我们仔细看看“空白”。

The differences are: (your vs. json.dumps)区别在于:(你的 vs. json.dumps)

  • "code:"123" vs. "code": "123" "code:"123""code": "123"
  • { "token"... vs. {"token"... { "token"...{"token"...
  • "token":{ "name"... vs. "token": {"name":.. "token":{ "name"... vs. "token": {"name":..

OR, you could compare two value with no spaces as like:或者,您可以比较两个没有空格的值,如下所示:

json.dumps(dict).replace(' ', '') == target.replace(' ', '')

It returns True as well.它也返回True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM