打印嵌套的JSON数据

时间:2019-03-16 20:17:53

标签: python json

我是编程新手。我正在处理使用请求库获得的一些数据。我将响应保存到一个单独的文件中。回复中有很多我不想打印的公司信息。我只需要某些类型的公司数据。

我遇到的问题是嵌套键/值。我可以很好地访问列表中第一家公司的键/值“营业年”,但问题是正在为数据文件中的每个公司复制列表中第一家公司的键/值。 / p>

以下是我正在使用的数据(为此帖子修改):

{
  "result": [
    {
      "info": [
        {
          "company_name": "Apples Company",
          "country_name": "America",
          "date_founded": "2018-04-01",
          "company_descr": "We sell apples.",
          "add_info": [
            {
              "years_in_business": "Over 20",
              "owns building": "Yes"
            }
          ],
          "add_data": [
            {
              "some_data_1_ID": "A0w8eed37rtfx",
              "some_data_2": "Value"
            }
          ]
        },
        {
          "company_name": "Car Company",
          "country_name": "Spain",
          "date_founded": "2012-08-09",
          "company_descr": "We sell cars",
          "add_info": [
            {
              "years_in_business": "Over 50",
              "owns building": "No"
            }
          ],
          "add_data": [
            {
              "some_data_1_ID": "Bsadf4trets77",
              "some_data_2": "No Value"
            }
          ]
        },
        {
          "company_name": "Pie Company",
          "country_name": "Germany",
          "date_founded": "1900-04-30",
          "company_descr": "We sell Pies.",
          "add_info": [
            {
              "years_in_business": "Over 100",
              "owns building": "Yes"
            }
          ],
          "add_data": [
            {
              "some_data_1_ID": "e64hfg6666",
              "some_data_2": "Value"
            }
          ]
        }
      ]
    }
  ]
}

这是我的代码:

import json

with open('company_info.json', encoding='utf8') as file:
    company_data = json.load(file)

for key in company_data['result'][0]['info']:
    print('Company Name: ' + key.get('company_name'))
    print('Country Name: ' + key.get('country_name'))
    print('Date Founded: ' + key.get('date_founded'))
    print('Years in Business: ' + company_data['result'][0]['info'][0]
    ['add_info'][0]['years_in_business'])

以下是输出:

Company Name: Apples Company
Country Name: America
Date Founded: 2018-04-01
Years in Business: Over 20
Company Name: Car Company
Country Name: Spain
Date Founded: 2012-08-09
Years in Business: Over 20
Company Name: Pie Company
Country Name: Germany
Date Founded: 1900-04-30
Years in Business: Over 20

Process finished with exit code 0

4 个答案:

答案 0 :(得分:0)

您可以使用enumerate来同时访问列表元素的indexvalue

for index, key in enumerate(company_data['result'][0]['info']):
    print('Company Name: ' + key.get('company_name'))
    print('Country Name: ' + key.get('country_name'))
    print('Date Founded: ' + key.get('date_founded'))
    print('Years in Business: ' + company_data['result'][0]['info'][index] 
    ['add_info'][0]['years_in_business'])

答案 1 :(得分:0)

您已希望从同一级别的键的列表中提取该值,以便其他键dict键为

for key in company_data['result'][0]['info']:
    print('Company Name: ' + key.get('company_name'))
    print('Country Name: ' + key.get('country_name'))
    print('Date Founded: ' + key.get('date_founded'))
    print('Years in Business: ' + key['add_info'][0]['years_in_business'])

答案 2 :(得分:0)

您应该像其他人一样迭代'years_in_business'

for key in company_data['result'][0]['info']:
    print('Company Name: ' + key.get('company_name'))
    print('Country Name: ' + key.get('country_name'))
    print('Date Founded: ' + key.get('date_founded'))
    print('Years in Business: ' + key.get('add_info')[0]['years_in_business'])

#Output:
Company Name: Apples Company
Country Name: America
Date Founded: 2018-04-01
Years in Business: Over 20
Company Name: Car Company
Country Name: Spain
Date Founded: 2012-08-09
Years in Business: Over 50
Company Name: Pie Company
Country Name: Germany
Date Founded: 1900-04-30
Years in Business: Over 100

答案 3 :(得分:0)

要打印所有公司的信息,请使用类似以下的命令对它们的列表进行迭代:

import json

with open('company_info.json', encoding='utf8') as file:
    company_data = json.load(file)

for company in company_data['result'][0]['info']:
    print('Company Name: ' + company.get('company_name'))
    print('Country Name: ' + company.get('country_name'))
    print('Date Founded: ' + company.get('date_founded'))
    print('Years in Business: '
            + company.get('add_info')[0]['years_in_business'])
    print()

输出:

Company Name: Apples Company
Country Name: America
Date Founded: 2018-04-01
Years in Business: Over 20

Company Name: Car Company
Country Name: Spain
Date Founded: 2012-08-09
Years in Business: Over 50

Company Name: Pie Company
Country Name: Germany
Date Founded: 1900-04-30
Years in Business: Over 100