python - 在 Python 中解析复杂且不断变化的 JSON 数据,深度多个级别

标签 python json list parsing dictionary

我正在尝试解析不断变化的 JSON 数据,但是 JSON 数据有点复杂,并且每次迭代都会发生变化。

JSON 数据在循环内解析,因此每次循环运行时,json 数据都是不同的。我现在关注的是教育数据。

JSON 数据:

第一个可能看起来像这样:

{u'gender': u'female', u'id': u'15394'}

下一个可能是:

{
u'gender': u'male', u'birthday': u'12/10/1983', u'location': {u'id': '12', u'name': u'Mexico City, Mexico'}, u'hometown': {u'id': u'19', u'name': u'Mexico City, Mexico'}, 

u'education': [
{
u'school': {u'id': u'22', u'name': u'Institut Saint Dominique de Rome'}, 
u'type': u'High School', 
u'year': {u'id': u'33', u'name': u'2002'}
}, 
{
u'school': {u'id': u'44', u'name': u'Instituto Cumbres'}, 
u'type': u'High School', 
u'year': {u'id': u'55', u'name': u'1999'}
}, 
{
u'school': {u'id': u'66', u'name': u'Chantemerle International School'},    
u'type': u'High School', 
u'year': {u'id': u'77', u'name': u'1998'}
}, 
{
u'school': {u'id': u'88', u'name': u'Columbia University'}, 
u'type': u'College', 
u'concentration': 
[{u'id': u'91', u'name': u'Economics'}, 
{u'id': u'92', u'name': u'Film Studies'}]
}
], 
u'id': u'100384'}

我正在尝试返回学校名称、学校 ID 和学校类型的所有值,因此本质上我想要 [education][school][id][education][school ][姓名][教育][学校][类型] 在一行中。然而,每个人列出的学校数量和类型不同,或者根本没有学校。我想在现有循环中的新行中返回每所学校及其关联的名称、ID 和类型。

理想输出:

1   34  Boston Latin School High School
1   26  Harvard University  College
1   22  University of Michigan  Graduate School

在本例中,它指的是friend_id,我已经将其设置为将其作为每个循环中的第一项附加到列表中。

我已经尝试过:

friend_data = response.read()
friend_json = json.loads(friend_data)

#This below is inside a loop pulling data for each friend:

try:
    for school_id in friend_json['education']:
        school_id = school_id['school']['id']
        friendedu.append(school_id)
    for school_name in friend_json['education']:
        school_name = school_name['school']['name']
        friendedu.append(school_name)
    for school_type in friend_json['education']:
        school_type = school_type['type']
        friendedu.append(school_type)
except:
    school_id = "NULL"

打印friendedu writer.writerow(friendedu)

电流输出:

[u'22'、u'44'、u'66'、u'88'、u'Institut Saint Dominique de Rome'、u'Instituto Cumbres'、u'Chantemerle 国际学校'、u '哥伦比亚大学',u'高中',u'高中',u'高中',u'学院']

此输出只是它所提取的值的列表,相反,我正在尝试组织输出,如上所示。我认为也许需要另一个 for 循环,因为对于一个人来说,我希望每所学校都有自己的路线。现在,friendedu 列表正在将一个人的所有教育信息附加到列表的每一行中。我希望每个教育项目都在一个新行中,然后转到下一个人并继续为下一个人写入行。

最佳答案

怎么样

friend_data = response.read()
friend_json = json.loads(friend_data)


if 'education' in friend_json.keys():
    for school_id in friend_json['education']:
        friendedu = []
        try:
            friendedu.append(school_id['school']['id'])
            friendedu.append(school_name['school']['name'])
            friendedu.append(school_type['school']['type'])
        except:
            friendedu.append('School ID, NAME, or type not found')
        print(" ".join(friendedu))

关于python - 在 Python 中解析复杂且不断变化的 JSON 数据,深度多个级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21802749/

相关文章:

python - 在 Plotly 的 Scattergeo 图中制作图例

python - 需要帮助理解 python 语法

javascript - 用于 Javascript/JSON 的 ORM

python - 变量将被多次实例化

python - 在 python 中使用 AWS Lambda 将 Post 请求发送到外部 API

javascript - 使用 JAVASCRIPT 从 HTML 上的 JSON 数组读取数据

javascript - 显示错误的动态生成表

c# - List<T> 是指针吗?

c# - 将值合并到一个列表中

java - Integer.parseInt 返回错误 : The method parseInt(String) in the type Integer is not applicable for the arguments (R. 字符串)