本帖最后由 Love3295292 于 2020-11-3 11:06 编辑
简单的遍历代码如下,希望能有所帮助:
[Python] 纯文本查看 复制代码 data = {
"Data": {
"Questions": [
{
"Number": 1,
"Content": "你觉得B真是因为爱Y才和TA结婚的吗?",
"Options": [
{
"Number": 1,
"Content": "是"
},
{
"Number": 2,
"Content": "不是"
}
]
},
{
"Number": 2,
"Content": "B出轨了但依然对Y很好,你认为TA更多是因为害怕,还是愧疚?",
"Options": [
{
"Number": 1,
"Content": "前者"
},
{
"Number": 2,
"Content": "后者"
}
]
},
{
"Number": 3,
"Content": "B重新爱上曾拒绝过自己的人,你觉得TA是出于以下哪种原因?",
"Options": [
{
"Number": 1,
"Content": "余情未了,深埋的感情重新复活"
},
{
"Number": 2,
"Content": "未完成情节,为证明自己的魅力"
}
]
}
]
}
}
if __name__ == '__main__':
questions = data['Data']['Questions']
print("qid\tnum\tcontent")
for question in questions: # 遍历第一层 -> 即Questions
qid = question['Number']
options = question['Options']
for option in options: # 遍历每个即Question中的Options
num = option['Number']
content = option['Content']
print("{}\t{}\t{}".format(qid, num, content))
执行结果示例:
|