print json对象使用python嵌套在数组中

时间:2022-03-16 21:42:39

I am working with a JSON file and am using Python. I am trying to print an object that is nested in an array. I would like to print select objects (e.g. "name", "thomas_id") from the following array (is it considered a 'list' of 'objects' in an array? would the array be called the "cosponsors" array?):

我正在使用JSON文件并使用Python。我正在尝试打印嵌套在数组中的对象。我想从下面的数组中打印选择对象(例如“name”,“thomas_id”)(它被认为是数组中“对象”的“列表”吗?数组是否会被称为“cosponsors”数组?):

"cosponsors": [
{
  "district": null, 
  "name": "Akaka, Daniel K.", 
  "sponsored_at": "2011-01-25", 
  "state": "HI", 
  "thomas_id": "00007", 
  "title": "Sen", 
  "withdrawn_at": null
}, 
.
.
.
  {
  "district": null, 
  "name": "Lautenberg, Frank R.", 
  "sponsored_at": "2011-01-25", 
  "state": "NJ", 
  "thomas_id": "01381", 
  "title": "Sen", 
  "withdrawn_at": null
}
] 

The problem is that I do not know the syntax to print objects (listed?) in an array. I have tried a number of variations extrapolated from what I have found on stack overflow; namely, variations of the following:

问题是我不知道在数组中打印对象(列出?)的语法。我已经尝试了一些从我在堆栈溢出中发现的变化;即,以下变化:

print(data['cosponsors']['0']['thomas_id']

I recieve the error "list indices must be integers or slices, not str"

我收到错误“列表索引必须是整数或切片,而不是str”

Background:

背景:

I have over 3000 json files that are contained within a so-called master file. I only need the same specific aspects of each file that I will need to later export into a MYSQL DB, but that is another topic (or is it, i.e. am I going about this the wrong way?). Accordingly, I am writing a code that I can impliment on all of the files in order to get the data that I need. I've been doing quite well, considering that I do not have any experience with programming. I have been using the following code in Python:

我有超过3000个json文件包含在一个所谓的主文件中。我只需要每个文件的相同特定方面,我将需要稍后导出到MYSQL数据库,但这是另一个主题(或者是,我是以错误的方式进行此操作?)。因此,我正在编写一个代码,我可以在所有文件上实现,以获取我需要的数据。考虑到我没有任何编程经验,我一直表现得很好。我一直在Python中使用以下代码:

import json

data = json.load(open('s2_data.json', 'r'))

print (data["official_title"], data["number"], data["introduced_at"], 
data["bill_id"], data['subjects_top_term'], data['subjects'], 
data['summary']['text'], data['sponsor']['thomas_id'], 
data['sponsor']['state'], data['sponsor']['name'],  data['sponsor'] 
['type'])

It has been returning results that are separated with a space. So far I am happy with that.

它一直在返回用空格分隔的结果。到目前为止,我很满意。

2 个解决方案

#1


1  

You are using a string to index the list, '0' is a string, not an integer. Remove the quotes:

您使用字符串索引列表,'0'是字符串,而不是整数。删除引号:

print(data['cosponsors'][0]['thomas_id'])

When in doubt, check the partial result; see what print(type(data['cosponsors'])) produces; if that produces <type 'list'>, you know you need to use indexing with integers, if you get <type 'dict'>, use keys (a list of which can be gotten by calling print(list(...)) on the dictionary), etc.

如有疑问,请检查部分结果;看看print(type('[cosponsors']))产生了什么;如果它产生 ,你知道你需要使用整数索引,如果你得到 ,使用键(可以通过调用print得到一个列表(list(...) )在字典上)等 'dict'> 'list'>

Usually, lists contain a variable number of objects; it could be just one, zero or a whole load more. You could loop over those objects:

通常,列表包含可变数量的对象;它可能只是一个,零个或整个负载。你可以遍历这些对象:

for cosponsor in data['cosponsors']:
    print(cosponsor['thomas_id'])

The loop sets cosponsor to each of the objects in the data['cosponsors'] list, one by one.

循环将cosponsor设置为数据['cosponsors']列表中的每个对象,逐个。

#2


1  

How about

怎么样

data['cosponsors'][0]['thomas_id']

Since a list has numeric indices.

由于列表具有数字索引。

#1


1  

You are using a string to index the list, '0' is a string, not an integer. Remove the quotes:

您使用字符串索引列表,'0'是字符串,而不是整数。删除引号:

print(data['cosponsors'][0]['thomas_id'])

When in doubt, check the partial result; see what print(type(data['cosponsors'])) produces; if that produces <type 'list'>, you know you need to use indexing with integers, if you get <type 'dict'>, use keys (a list of which can be gotten by calling print(list(...)) on the dictionary), etc.

如有疑问,请检查部分结果;看看print(type('[cosponsors']))产生了什么;如果它产生 ,你知道你需要使用整数索引,如果你得到 ,使用键(可以通过调用print得到一个列表(list(...) )在字典上)等 'dict'> 'list'>

Usually, lists contain a variable number of objects; it could be just one, zero or a whole load more. You could loop over those objects:

通常,列表包含可变数量的对象;它可能只是一个,零个或整个负载。你可以遍历这些对象:

for cosponsor in data['cosponsors']:
    print(cosponsor['thomas_id'])

The loop sets cosponsor to each of the objects in the data['cosponsors'] list, one by one.

循环将cosponsor设置为数据['cosponsors']列表中的每个对象,逐个。

#2


1  

How about

怎么样

data['cosponsors'][0]['thomas_id']

Since a list has numeric indices.

由于列表具有数字索引。