这是有效载荷
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排序)。
答案 0 :(得分:0)
首先,您提供的JSON无效。我已对您的答案进行了编辑,以提供有效答案。
回答您的问题:
ast.literal_eval()
将字符串转换为字典。 您可以申请:
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
中的lambda
与key
一起使用
例如:
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))