json - 遍历嵌套的 JSON 对象并使用 Python 获取值

标签 json python-3.x for-loop

我正在使用 Python;我需要遍历 JSON 对象并检索嵌套值。我的数据片段如下:

 "bills": [
{
  "url": "http:\/\/maplight.org\/us-congress\/bill\/110-hr-195\/233677",
  "jurisdiction": "us",
  "session": "110",
  "prefix": "H",
  "number": "195",
  "measure": "H.R. 195 (110\u003csup\u003eth\u003c\/sup\u003e)",
  "topic": "Seniors' Health Care Freedom Act of 2007",
  "last_update": "2011-08-29T20:47:44Z",
  "organizations": [
    {
      "organization_id": "22973",
      "name": "National Health Federation",
      "disposition": "support",
      "citation": "The National Health Federation (n.d.). \u003ca href=\"http:\/\/www.thenhf.com\/government_affairs_federal.html\"\u003e\u003ccite\u003e Federal Legislation on Consumer Health\u003c\/cite\u003e\u003c\/a\u003e. Retrieved August 6, 2008, from The National Health Federation.",
      "catcode": "J3000"
    },
    {
      "organization_id": "27059",
      "name": "A Christian Perspective on Health Issues",
      "disposition": "support",
      "citation": "A Christian Perspective on Health Issues (n.d.). \u003ca href=\"http:\/\/www.acpohi.ws\/page1.html\"\u003e\u003ccite\u003ePart E - Conclusion\u003c\/cite\u003e\u003c\/a\u003e. Retrieved August 6, 2008, from .",
      "catcode": "X7000"
    },
    {
      "organization_id": "27351",
      "name": "Natural Health Roundtable",
      "disposition": "support",
      "citation": "Natural Health Roundtable (n.d.). \u003ca href=\"http:\/\/naturalhealthroundtable.com\/reform_agenda\"\u003e\u003ccite\u003eNatural Health Roundtable SUPPORTS the following bills\u003c\/cite\u003e\u003c\/a\u003e. Retrieved August 6, 2008, from Natural Health Roundtable.",
      "catcode": "J3000"
    }
  ]
},

我需要通过“账单”中的每个对象并获得“ session ”、“前缀”等,我还需要通过每个“组织”并获得“名称”、“处置”等。我有以下代码:
import csv
import json

path = 'E:/Thesis/thesis_get_data'

with open (path + "/" + 'maplightdata110congress.json',"r") as f:
data = json.load(f)
a = data['bills']
b = data['bills'][0]["prefix"]
c = data['bills'][0]["number"]

h = data['bills'][0]['organizations'][0]
e = data['bills'][0]['organizations'][0]['name']
f = data['bills'][0]['organizations'][0]['catcode']
g = data['bills'][0]['organizations'][0]['catcode']

for i in a:
    for index in e:
          print ('name')

它会多次返回字符串“name”。

建议?

最佳答案

我在另一个论坛上找到了解决方案,并希望与这里的所有人分享,以防有人再次遇到这种情况。

import csv
import json

path = 'E:/Thesis/thesis_get_data'

with open (path + "/" + 'maplightdata110congress.json',"r") as f:
data = json.load(f)

for bill in data['bills']:
    for organization in bill['organizations']:
        print (organization.get('name'))`

关于json - 遍历嵌套的 JSON 对象并使用 Python 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34818782/

相关文章:

json - 使用 JSON-B 1.0(例如 Yasson、Java EE 8)有效地将 JsonObject 转换为 pojo

python - 来自迭代多个 URL 的 JSON 响应以存储 DataFrame

java - 使用 Json 在 Android 客户端和 WCF 服务之间传递 DateTime 作为参数

python - Django objects.all() 不显示任何内容

Scala连接循环结果

c++ - 在 for 循环中删除 vector 中的对象时,如何防止发生段错误?

c# - 序列化派生列表的列表

Python 套接字 [WinError 10057]?

java - 为什么两个不同的嵌套循环(具有相同的时间复杂度)执行时间不同?

python - 将 pandas 过滤器应用于数据帧会得到一个充满 NaN 的数据帧