如何将json字符串转换为字典并保存键中的顺序? [英] How to convert json string to dictionary and save order in keys?

查看:472
本文介绍了如何将json字符串转换为字典并保存键中的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想将json字符串转换为Python字典时遇到问题.我有

I have problem when I want to convert json string to Python dictionary. I have string like

 s={"name":{"Saban:Saulic"},"price":{"koncert:1000"} ....}

当我写类似

tags=json.loads(s)

我gtet字典,但键的顺序与字符串中的顺序不同(它不是名称,价格...).如何将json字符串转换为字典并保存键中的顺序?

I gtet dictionary but order of keys is not the same like in string ( it is not name, price ...). How to convert json string to dictionary and save order in keys ?

推荐答案

Python 2.7 您从 collections

这种字典保留了元素的插入顺序.

This kind of dictionary preserves the insertion order of elements.

来自Python文档:

From Python docs:

json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

反序列化fp(支持.read()的类似文件的对象,其中包含 JSON文档)到Python对象.

Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object.

如果fp的内容使用基于ASCII的编码进行编码,则other 而不是UTF-8(例如latin-1),则必须使用适当的编码名称 指定的.不是基于ASCII的编码(例如UCS-2)不是 允许,并且应使用codecs.getreader(encoding)(fp)进行包装,或者 只需将其解码为unicode对象,然后传递给loads().

If the contents of fp are encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed, and should be wrapped with codecs.getreader(encoding)(fp), or simply decoded to a unicode object and passed to loads().

object_hook是一个可选函数,将与 任何对象文字被解码(字典)的结果.的返回值 将使用object_hook代替dict.可以使用此功能 来实现自定义解码器(例如JSON-RPC类提示).

object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).

object_pairs_hook是一个可选函数,将与 用对的有序列表解码的任何对象文字的结果. 将使用object_pairs_hook的返回值代替 字典此功能可用于实现依赖于 按照键和值对被解码的顺序(例如, collections.OrderedDict()将记住插入顺序).如果 还定义了object_hook,以object_pairs_hook为优先.

object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, collections.OrderedDict() will remember the order of insertion). If object_hook is also defined, the object_pairs_hook takes priority.

我认为您可以将object_pairs_hook参数与 collections.OrderedDict()

I think you could use object_pairs_hook parameter with collections.OrderedDict()

tags=json.loads(s, object_pairs_hook=collections.OrderedDict)

这篇关于如何将json字符串转换为字典并保存键中的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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