从python中的json文件中提取数据

从python中的json文件中提取数据

问题描述:

我一直试图找到搜索特定值并提取数据的最佳方法.我是新来的,希望我的措词正确.

I have been try to find the best way to search a particular value and extract the data. I am new and hope i have worded this correctly.

    {"family": {
    "name": "Mary",
    "age": "32",
    "sex": "female",
    "kids": [
      {
        "name": "jim",
        "age": "10",
        "sex": "male",
        "dob_year": "2007",
        "ssn": "123-23-1234"
      },
      {
        "name": "jill",
        "age": "6",
        "sex": "female",
        "dob_year": "2011",
        "ssn": "123-23-1235"
      }]}}

如果我需要搜索包含"1234"的"ssn"并返回"name""jim",那么实现此目标的最佳方法是什么?

if i needed to search "ssn" containing "1234" and return the "name" "jim" what would be the best way to go about achieving this?

实际上,问题在于从文件

所以您在这里:

import json

with open('data.json') as json_file:
    data = json.load(json_file)
    for kid in data['family']['kids']:
        if '1234' in kid['ssn']:
            print(kid['name'])

其他阅读内容: https://stackabuse .com/reading-and-writing-json-to-a-file-in-python/