Python中的JSON输出排序 [英] JSON output sorting in Python

查看:861
本文介绍了Python中的JSON输出排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中遇到JSON问题.

I've a problem with JSON in python.

实际上,如果我尝试执行此代码,则python给了我一个排序后的JSON字符串!

In fact, if I try to execute this code, python gives me a sorted JSON string!

例如:

values = {'profile' : 'testprofile', 
          'format': 'RSA_RC4_Sealed', 
          'enc_key' : base64.b64encode(chiave_da_inviare), 
          'request' : base64.b64encode(data)
      }


values_json = json.dumps(values, sort_keys=False, separators=(',', ':'))

这是输出:

{"profile":"testprofile","enc_key":"GBWo[...]NV6w==","request":"TFl[...]uYw==","format":"RSA_RC4_Sealed"}

如您所见,我尝试使用"sort_keys = False",但未做任何更改.

As you can see, I tried to use "sort_keys=False" but nothing changed.

如何停止Python对JSON字符串进行排序?

How can I stop Python sorting my JSON strings?

推荐答案

您正在将值存储在python中 dict 根本没有内在的顺序概念,它只是一个键=>值映射.因此,当您将它们放入值"变量中时,它们将失去所有排序.

You are storing your values into a python dict which has no inherent notion of ordering at all, it's just a key => value map. So your items lose all ordering when you place them into the "values" variable.

实际上,获得确定性排序的唯一方法是使用"sort_keys = True",我假设它们以字母数字顺序放置.为什么订单如此重要?

In fact the only way to get a deterministic ordering would be to use "sort_keys=True", which I assume places them in alphanumeric ordering. Why is the order so important?

这篇关于Python中的JSON输出排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆