简体   繁体   English

解析python中的json数据列表

[英]Parsing json data list in python

I'm a beginner coding a home project.我是一个编写家庭项目的初学者。 I've used json to store some information and now I've opened it from another python function to use, here is what is returned:我已经使用 json 来存储一些信息,现在我已经从另一个 python function 打开它来使用,这是返回的内容:

{'_id': {'$oid': '5eab443d1eae7f1c689ab614'}, 'a': 'James', 'answer1': 'c', 'answer2': 'a', 'answer3': 'b', 'answer4': 'c', 'answer5': 'b', 'b': 'John', 'c': 'Jake', 'd': 'a house', 'e': 'a caravan', 'f': 'a hotel', 'g': 'red', 'h': 'orange', 'i': 'yellow', 'j': 'hat', 'k': 'scarf', 'l': "gloves", 'm': 'happy', 'n': 'sad', 'o': 'upset', 'q1': '1', 'q2': '2', 'q3': '3', 'q4': '4', 'q5': '5', 'question1': 'The boy's name was:', 'question2': 'The boy lived in:', 'question3': 'The boy's jumper was:', 'question4': 'The boy lost his:', 'question5': 'The boy was:', 'quiz_id': '1a', 'quiz_title': 'The New Boy', 'url': '/images/comp_cards/card1a.png'}

Now, I am trying to pull information from this list but all the info is stored at the 0 index so when I do this:现在,我正在尝试从此列表中提取信息,但所有信息都存储在 0 索引中,所以当我这样做时:

with open('quiz.json', 'r') as read_file: 
    quiz_data = json.load(read_file) 

    print(quiz_data[0])

the whole lot is returned, if I change the index value to above 0 then nothing is returned.全部返回,如果我将索引值更改为 0 以上,则不返回任何内容。 What do I need to do to retrieve only data from the list that I need, so say I need the fields name 'answer1', 'answer2' and 'answer3'?我需要做什么才能只从列表中检索我需要的数据,比如我需要字段名称“answer1”、“answer2”和“answer3”? thanks谢谢

since output is a dictionary,you can access to key and get the value.For example if you need value of answer1 you can try由于 output 是字典,您可以访问键并获取值。例如,如果您需要 answer1 的值,您可以尝试

quiz_data[0].get("answer1")

or或者

quiz_data[0]["answer1"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM