按值排序 JSON

Sorting JSON by value

我正在尝试使用 Python

解析以下学生成绩报告 JSON
{
    "report":[
        {
            "enrollment": "rit2011001",
            "name": "Julia",
            "subject":[
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        },
        {
            "enrollment": "rit2011020",
            "name": "Samantha",
            "subject":[
                {
                    "code": "COM",
                    "grade": "B"
                },
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        }
    ]
}

这样报告应该先按代码升序排列,然后按年级升序排列,然后按入学率升序排列。输出应该是这样的

COM B rit2011020 Samantha
DSA A rit2011001 Julia
DSA A rit2011020 Samantha

这是我需要帮助的不完整代码:

import json

data='''{
    "report":[
        {
            "enrollment": "rit2011001",
            "name": "Julia",
            "subject":[
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        },
        {
            "enrollment": "rit2011020",
            "name": "Samantha",
            "subject":[
                {
                    "code": "COM",
                    "grade": "B"
                },
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        }
    ]
}'''

print data  #for debug
parsed_json = json.loads(data)
print parsed_json #for debug
for key,value in sorted(parsed_json.items()):
    print key,value

我不知道如何应用连续过滤来获得结果。

尝试使用嵌套循环,print:

import json
data='''{
    "report":[
        {
            "enrollment": "rit2011001",
            "name": "Julia",
            "subject":[
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        },
        {
            "enrollment": "rit2011020",
            "name": "Samantha",
            "subject":[
                {
                    "code": "COM",
                    "grade": "B"
                },
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        }
    ]
}'''
print data  #for debug
parsed_json = json.loads(data)
print parsed_json #for debug
for i in parsed_json['report']:
    for x in i['subject']:
        print x['code'],x['grade'],i['enrollment'],i['name']

输出:

DSA A rit2011001 Julia
COM B rit2011020 Samantha
DSA A rit2011020 Samantha

如果关心帧的顺序:

import json
data='''{
    "report":[
        {
            "enrollment": "rit2011001",
            "name": "Julia",
            "subject":[
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        },
        {
            "enrollment": "rit2011020",
            "name": "Samantha",
            "subject":[
                {
                    "code": "COM",
                    "grade": "B"
                },
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        }
    ]
}'''
print data  #for debug
parsed_json = json.loads(data)
print parsed_json #for debug
l=[]
for i in parsed_json['report']:
    for x in i['subject']:
        l.append(' '.join([x['code'],x['grade'],i['enrollment'],i['name']]))
print('\n'.join(sorted(l)))

如果你愿意使用非常流行的外部库进行数据分析,那么你可以使用pandas with json_normalize(),例如:

In []:
from pandas.io.json import json_normalize

df = json_normalize(parsed_json['report'], 'subject', ['enrollment', 'name'])
df.sort_values(['code', 'grade', 'enrollment']).reset_index(drop=True)

Out[]:
  code grade  enrollment      name
0  COM     B  rit2011020  Samantha
1  DSA     A  rit2011001     Julia
2  DSA     A  rit2011020  Samantha