在python中创建自定义json排序顺序

时间:2015-10-15 00:18:28

标签: python json

我有这个script,我有一个自定义的json示例。我需要获取完整的json并将其保存在同一位置的文件中。

dic = {'window': set_window, 'window.screen': window_screen, 'window.navigator' : window_navigator }

json=json.dumps(dic, indent=5, sort_keys=False)
print json

此刻的订单是正确的,我需要以相同的顺序打印。我已经尝试了2天但它仍然失败:((是否可以按我的意愿保留JSON对象的顺序?

2 个答案:

答案 0 :(得分:1)

  

我需要获取完整的json并将其保存在同一个文件中   位置。

您不应该依赖职位来更新包含JSON的文件。

首先,将您在JSON中存储的配置与实际代码分开是明智的。

所以,让我们说你有一个包含JSON的文件:

<强> config.json

{
     "window": {
          "innerHeight": 886, 
          "innerWidth": 1280
     }, 
     "window.screen": {
          "availWidth": 1280, 
          "availTop": 0, 
          "availHeight": 1024, 
          "availLeft": 0
     }, 
     "window.navigator": {
          "oscpu": "Windows NT 6.3; WOW64", 
          "product": "Gecko", 
          "vendor": "", 
          "buildID": "20140923175406", 
          "appName": "Netscape", 
          "appCodeName": "Mozilla", 
          "productSub": "20100101", 
          "plugins": {
               "1": {
                    "application/sdp": {
                         "type": "application/sdp", 
                         "description": "Playing SDP-files", 
                         "suffixes": "sdp"
                    }, 
                    "name": "QuickTime Plug-in 7.7.5", 
                    "filename": "npqtplugin.dll", 
                    "0": {
                         "type": "application/sdp", 
                         "description": "Playing SDP-files", 
                         "suffixes": "sdp"
                    }, 
                    "length": 1, 
                    "version": "7.7.7.5", 
                    "description": "The QuickTime Plugin allows you to view a wide variety of multimedia content in Web pages. For more information, visit the QuickTime Web site."
               }, 
               "0": {
                    "1": {
                         "type": "application/futuresplash", 
                         "description": "FutureSplash movie", 
                         "suffixes": "spl"
                    }, 
                    "0": {
                         "type": "application/x-shockwave-flash", 
                         "description": "Adobe Flash movie", 
                         "suffixes": "swf"
                    }, 
                    "length": 2, 
                    "version": "12.0.0.43", 
                    "name": "Shockwave Flash", 
                    "application/futuresplash": {
                         "type": "application/futuresplash", 
                         "description": "FutureSplash movie", 
                         "suffixes": "spl"
                    }, 
                    "application/x-shockwave-flash": {
                         "type": "application/x-shockwave-flash", 
                         "description": "Adobe Flash movie", 
                         "suffixes": "swf"
                    }, 
                    "filename": "NPSWF32_12_0_0_43.dll", 
                    "description": "Shockwave Flash 12.0 r.43"
               }, 
               "length": 2, 
               "Shockwave Flash": {
                    "application/futuresplash": {
                         "description": "FutureSplash movie", 
                         "suffixes": "spl"
                    }, 
                    "description": "Shockwave Flash 12.0 r.43", 
                    "application/x-shockwave-flash": {
                         "description": "Adobe Flash movie", 
                         "suffixes": "swf"
                    }, 
                    "filename": "NPSWF32_12_0_0_43.dll", 
                    "version": "12.0.0.43", 
                    "name": "Shockwave Flash"
               }
          }, 
          "userAgent": "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0.3", 
          "language": "en", 
          "appVersion": "5.0 (Windows)", 
          "platform": "x86", 
          "vendorSub": ""
     }
}

更新存储在文件中的JSON需要做的是读取文件,使用json.loads将JSON加载到python dict中,更新您可能需要的任何内容并保存输出json.dumps的更新密钥和值到同一文件。因此,您最终会在代码中的某处出现类似内容:

import json

with open('prueba.json', 'r') as f:
    config = json.loads(f.read())

with open('prueba.json', 'w') as f:
    # Here I update window settings as an example
    config['window']['innerHeight'] = 1440
    config['window']['innerWidth'] = 900
    f.write(json.dumps(config))

答案 1 :(得分:0)

以下是如何完成的基本想法:

class CustomOrderStr(str):

    def __lt__(self, other):
        # Replace with whatever comparison logic you want. This just reverses order.
        return not str.__lt__(self, other)

dic = {'a': 1, 'b': 2, 'c': 3}

print json.dumps(dic, indent=5, sort_keys=True)
print json.dumps({CustomOrderStr(key): value for key, value in dic.iteritems()}, indent=5, sort_keys=True)

输出:

{
     "a": 1, 
     "b": 2, 
     "c": 3
}
{
     "c": 3, 
     "b": 2, 
     "a": 1
}

但是我必须同意其他人的意见,如果你想要一个JSON的自定义订单,你可能会做坏事。