我在使用python从文件中读取json时遇到问题.数据存储在未排序的字典中.我想将列表变量中的数据存储为正确的顺序.
flookup = open('lookup.json') self.tags = json.loads(flookup.read()) flookup.close()
self.tags包含未根据lookup.json文件排序的数据
{ "JournalTitle": "Journal Title", "PubCode": "Pub Code", "UniqueDocumentID": "Unique Document ID" }
Paul Draper.. 9
import collections, json data = '{"a":1, "b": 2}' print json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(data)
版画
OrderedDict([(u'a', 1), (u'b', 2)])
(虽然要求这样的东西似乎有点奇怪.如果顺序很重要,JSON数据应该在一个数组中.)