遍历嵌套的 JSON 对象并使用 Python 获取值
Posted
技术标签:
【中文标题】遍历嵌套的 JSON 对象并使用 Python 获取值【英文标题】:iterate through nested JSON object and get values with Python 【发布时间】:2016-04-21 12:03:16 【问题描述】:我正在使用 Python;我需要遍历 JSON 对象并检索嵌套值。我的数据的 sn-p 如下:
"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"
]
,
我需要遍历“票据”中的每个对象并获得“会话”、“前缀”等,我还需要遍历每个“组织”并获得“名称”、“处置”等。我有以下代码:
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”。
建议?
【问题讨论】:
【参考方案1】:我在另一个论坛上找到了解决方案,并想在这里与大家分享,以防有人再次遇到这种情况。
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'))`
【讨论】:
【参考方案2】:这可能会对你有所帮助。
def func1(data):
for key,value in data.items():
print (str(key)+'->'+str(value))
if type(value) == type(dict()):
func1(value)
elif type(value) == type(list()):
for val in value:
if type(val) == type(str()):
pass
elif type(val) == type(list()):
pass
else:
func1(val)
func1(data)
您所要做的就是将 JSON 对象作为字典传递给函数。
还有这个 python 库可以帮助你。你可以在这里找到这个 -> JsonJ
和平兄弟!!!
【讨论】:
得到一些输出,但大多是错误:for key,value in data.items(): AttributeError: 'unicode' object has no attribute 'items' @Shaze,你能不能请你交叉检查数据是 dict 类型还是其他类型..【参考方案3】:这个问题是双重嵌套的,所以两个for loops
有意义。
以下是 Pluralsight 使用他们的 GraphGL 的摘录,其中包含一个深入三个级别以获取进度、用户或课程信息的示例:
"data":
"courseProgress":
"nodes": [
"user":
"id": "1",
"email": "a@a.com",
"startedOn": "2019-07-26T05:00:50.523Z"
,
"course":
"id": "22",
"title": "Building Machine Learning Models in Python with scikit-learn"
,
"percentComplete": 34.0248,
"lastViewedClipOn": "2019-07-26T05:26:54.404Z"
]
解析这个 JSON 的代码:
for item in items["data"]["courseProgress"]["nodes"]:
print(item["user"].get('email'))
print(item["course"].get('title'))
print(item.get('percentComplete'))
print(item.get('lastViewedClipOn'))
【讨论】:
【参考方案4】:细化为@Joish's answer
def func1(data):
for key,value in data.items():
print (str(key)+'->'+str(value))
if isinstance(value, dict):
func1(value)
elif isinstance(value, list):
for val in value:
if isinstance(val, str):
pass
elif isinstance(val, list):
pass
else:
func1(val)
func1(data)
同实现here
【讨论】:
以上是关于遍历嵌套的 JSON 对象并使用 Python 获取值的主要内容,如果未能解决你的问题,请参考以下文章
PHP 循环遍历嵌套的 JSON 响应并重新组装为 Webhook 的简单查询字符串