Python的json模块,将int字典键转换为字符串 [英] Python's json module, converts int dictionary keys to strings

查看:695
本文介绍了Python的json模块,将int字典键转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现运行以下命令时,python的json模块(自2.6起包含)将int字典键转换为字符串.

I have found that when the following is run, python's json module (included since 2.6) converts int dictionary keys to strings.

>>> import json
>>> releases = {1: "foo-v0.1"}
>>> json.dumps(releases)
'{"1": "foo-v0.1"}'

有什么简单的方法可以将键保留为int形式,而无需在转储和加载时解析字符串. 我相信可以使用json模块提供的钩子,但这仍然需要解析. 我可能会忽略一个论点吗? 欢呼声,查兹

Is there any easy way to preserve the key as an int, without needing to parse the string on dump and load. I believe it would be possible using the hooks provided by the json module, but again this still requires parsing. Is there possibly an argument I have overlooked? cheers, chaz

子问题: 感谢您的回答.看到j​​son就像我所担心的那样工作,是否有一种简单的方法可以通过解析转储的输出来传达密钥类型? 另外我还要注意执行转储的代码以及从服务器下载json对象并加载它的代码都是我编写的.

Sub-question: Thanks for the answers. Seeing as json works as I feared, is there an easy way to convey key type by maybe parsing the output of dumps? Also I should note the code doing the dumping and the code downloading the json object from a server and loading it, are both written by me.

推荐答案

这是可能会咬你的各种映射集合之间的细微差别之一.

This is one of those subtle differences among various mapping collections that can bite you.

在Python中(显然在Lua中),映射的键(分别是字典或表)是对象引用.在Python中,它们必须是不可变的类型,或者它们必须是实现__hash__方法的对象. (Lua文档建议,即使对于可变对象,它也会自动将对象的ID用作哈希/键,并依赖于字符串插值以确保等效的字符串映射到相同的对象.)

In Python (and apparently in Lua) the keys to a mapping (dictionary or table, respectively) are object references. In Python they must be immutable types, or they must be objects which implement a __hash__ method. (The Lua docs suggest that it automatically uses the object's ID as a hash/key even for mutable objects and relies on string interning to ensure that equivalent strings map to the same objects).

在Perl,Javascript,awk和许多其他语言中,散列,关联数组或给定语言所调用的名称的键是字符串(或Perl中的标量").在perl $foo{1}, $foo{1.0}, and $foo{"1"}中,所有对%foo中相同映射的引用---键被作为标量进行求值

In Perl, Javascript, awk and many other languages the keys for hashes, associative arrays or whatever they're called for the given language, are strings (or "scalars" in Perl). In perl $foo{1}, $foo{1.0}, and $foo{"1"} are all references to the same mapping in %foo --- the key is evaluated as a scalar!

JSON开始作为Javascript序列化技术. (JSON表示[J] ava [S] cript [o] bject [n] otation.)自然,它为其映射表示法实现了与映射语义一致的语义.

JSON started as a Javascript serialization technology. (JSON stands for [J]ava[S]cript [o]bject [n]otation.) Naturally it implements semantics for its mapping notation which are consistent with its mapping semantics.

如果序列化的两端都将是Python,那么最好使用酱菜.如果您确实需要将这些从JSON转换回本机Python对象,我想您有两种选择.首先,如果字典查找失败,您可以尝试(try: ... except: ...)将任何键转换为数字.或者,如果将代码添加到另一端(此JSON数据的序列化器或生成器),则可以让它对每个键值执行JSON序列化---将其作为键列表提供. (然后,您的Python代码将首先在键列表上进行迭代,将它们实例化/反序列化为本地Python对象...,然后使用那些键来访问映射中的值).

If both ends of your serialization are going to be Python then you'd be better off using pickles. If you really need to convert these back from JSON into native Python objects I guess you have a couple of choices. First you could try (try: ... except: ...) to convert any key to a number in the event of a dictionary look-up failure. Alternatively, if you add code to the other end (the serializer or generator of this JSON data) then you could have it perform a JSON serialization on each of the key values --- providing those as a list of keys. (Then your Python code would first iterate over the list of keys, instantiating/deserializing them into native Python objects ... and then use those for access the values out of the mapping).

这篇关于Python的json模块,将int字典键转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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