我已经写了这段代码:
import json
window_screen = {"window.screen":
{"availHeight": 1080,
"availLeft": 1920,
"availWidth": 1920,
"clientHeight": 1080,
"colorDepth": 32,
"height": 1080,
"offsetWidth": 1920,
"pixelDepth": 32,
"top": 1080,
"width": 1920}}
window_navigator = {"window.navigator":
{"appVersion": "",
"product": "",
"vendor": "",
"vendorSub": "",
"online": "",
"appName": "",
"appCodeName": "",
"oscpu": "",
"userAgent": "",
"language": "",
"languages":"",
"platform":""}}
data = dict(window_screen, **window_navigator)
print json.dumps(data, indent=3)
该脚本有效,但对列表进行排序。我需要按照上面写的顺序打印它们。
需要输出:
{
"window.screen": {
"availHeight": 1080,
"availLeft": 1920,
"availWidth": 1920,
"clientHeight": 1080,
"colorDepth": 32,
"height": 1080,
"offsetWidth": 1920,
"pixelDepth": 32,
"top": 1080,
"width": 1920,
},
"window.navigator": {
"appVersion": "",
"product": "",
"vendor": "",
"vendorSub": "",
"online": ""
"appName": "",
"appCodeName": "",
"oscpu": "",
"userAgent": "",
"language": "",
"languages": "",
"platform": ""
}
}
我需要这个输出,没有排序的属性,怎么能做到这一点?
答案 0 :(得分:0)
Python词典不维护顺序,因此您无法保证您输入数据的顺序与从json.dumps()
发出的顺序相同。如果您需要有序字典,请查看collections.OrderedDict
。