使用JSON作为输入来生成排序的嵌套字典

时间:2018-07-21 14:20:27

标签: python json

这是有效载荷

     payload = '''
{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": [
                {
                    "ID": "ABCD",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                },
                {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                },
                {
                    "ID": "PQRS",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                },
                {
                    "ID": "ABRA",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            ]
        }
    }
}

'''

以上面的json字符串作为输入,并打印输出的JSON,其中对GlossList进行了(按ID排序)

3 个答案:

答案 0 :(得分:0)

首先,您提供的JSON无效。我已对您的答案进行了编辑,以提供有效答案。

回答您的问题:

  1. 您必须使用ast.literal_eval()将字符串转换为字典。
  2. 您可以申请:

    2.1 sorted函数,如果是的话,则必须构建原始字典,但这次是排序的。

    2.2 sort方法

应用sorted功能

payload = ast.literal_eval(json_string)
sub_order_dict = sorted(payload["glossary"]["GlossDiv"]["GlossList"], key=lambda x: x["ID"])
d = {'glossary': {'title': 'example glossary',
     'GlossDiv': {'title': 'S',
           'GlossList': sub_order_dit}}}

应用sort方法

payload = ast.literal_eval(json_string)
payload["glossary"]["GlossDiv"]["GlossList"].sort(key=lambda x: x["ID"])

在两种情况下,payload的输出为:

{'glossary': {'title': 'example glossary',
  'GlossDiv': {'title': 'S',
   'GlossList': [{'ID': 'ABCD',
     'SortAs': 'SGML',
     'GlossTerm': 'Standard Generalized Markup Language',
     'Acronym': 'SGML',
     'Abbrev': 'ISO 8879:1986',
     'GlossDef': {'para': 'A meta-markup language, used to create markup languages such as DocBook.',
      'GlossSeeAlso': ['GML', 'XML']},
     'GlossSee': 'markup'},
    {'ID': 'ABRA',
     'SortAs': 'SGML',
     'GlossTerm': 'Standard Generalized Markup Language',
     'Acronym': 'SGML',
     'Abbrev': 'ISO 8879:1986',
     'GlossDef': {'para': 'A meta-markup language, used to create markup languages such as DocBook.',
      'GlossSeeAlso': ['GML', 'XML']},
     'GlossSee': 'markup'},
    {'ID': 'PQRS',
     'SortAs': 'SGML',
     'GlossTerm': 'Standard Generalized Markup Language',
     'Acronym': 'SGML',
     'Abbrev': 'ISO 8879:1986',
     'GlossDef': {'para': 'A meta-markup language, used to create markup languages such as DocBook.',
      'GlossSeeAlso': ['GML', 'XML']},
     'GlossSee': 'markup'},
    {'ID': 'SGML',
     'SortAs': 'SGML',
     'GlossTerm': 'Standard Generalized Markup Language',
     'Acronym': 'SGML',
     'Abbrev': 'ISO 8879:1986',
     'GlossDef': {'para': 'A meta-markup language, used to create markup languages such as DocBook.',
      'GlossSeeAlso': ['GML', 'XML']},
     'GlossSee': 'markup'}]}}}

答案 1 :(得分:0)

如果您只需要对GlossList进行排序,请尝试将sorted中的lambdakey一起使用

例如:

print( sorted(payload["glossary"]["GlossDiv"]["GlossList"], key=lambda x: x["ID"]) )

输出:

[{'Abbrev': 'ISO 8879:1986',
  'Acronym': 'SGML',
  'GlossDef': {'GlossSeeAlso': ['GML', 'XML'],
               'para': 'A meta-markup language, used to create markup languages such as DocBook.'},
  'GlossSee': 'markup',
  'GlossTerm': 'Standard Generalized Markup Language',
  'ID': 'ABCD',
  'SortAs': 'SGML'},
 {'Abbrev': 'ISO 8879:1986',
  'Acronym': 'SGML',
  'GlossDef': {'GlossSeeAlso': ['GML', 'XML'],
               'para': 'A meta-markup language, used to create markup languages such as DocBook.'},
  'GlossSee': 'markup',
  'GlossTerm': 'Standard Generalized Markup Language',
  'ID': 'ABRA',
  'SortAs': 'SGML'},
 {'Abbrev': 'ISO 8879:1986',
  'Acronym': 'SGML',
  'GlossDef': {'GlossSeeAlso': ['GML', 'XML'],
               'para': 'A meta-markup language, used to create markup languages such as DocBook.'},
  'GlossSee': 'markup',
  'GlossTerm': 'Standard Generalized Markup Language',
  'ID': 'PQRS',
  'SortAs': 'SGML'},
 {'Abbrev': 'ISO 8879:1986',
  'Acronym': 'SGML',
  'GlossDef': {'GlossSeeAlso': ['GML', 'XML'],
               'para': 'A meta-markup language, used to create markup languages such as DocBook.'},
  'GlossSee': 'markup',
  'GlossTerm': 'Standard Generalized Markup Language',
  'ID': 'SGML',
  'SortAs': 'SGML'}]

答案 2 :(得分:0)

width

输出

>>> import json
>>> d = json.loads(payload)
>>> d['glossary']['GlossDiv']['GlossList'].sort(key=lambda x: x['ID'])
>>> print (json.dumps(d, indent=4))