如何用Python解析出嵌套的JSON
下面是我必须获得JSON的代码如何用Python解析出嵌套的JSON,python,json,Python,Json,下面是我必须获得JSON的代码 lite = requests.get("https://app.v.com/api/v2/4dfasdfasdfadsfasdfasdfasdf/keywords/list?site_id=000821&format=json") 这就是它看起来的样子 {'Response': {'responsecode': '200', 'resultsreturned': '100', 'totalresults': '1021', 'nextpage': '
lite = requests.get("https://app.v.com/api/v2/4dfasdfasdfadsfasdfasdfasdf/keywords/list?site_id=000821&format=json")
这就是它看起来的样子
{'Response':
{'responsecode': '200', 'resultsreturned': '100', 'totalresults': '1021', 'nextpage': '/keywords/list?site_id=000000&start=100&format=json',
'Result': [
{'Id': '18540409', 'Keyword': blindness', 'KeywordMarket': 'US-en', 'KeywordLocation': None, 'KeywordDevice': 'desktop', 'KeywordTranslation': None, 'KeywordTags': 'banner blindness - 10/18', 'KeywordStats': {'AdvertiserCompetition': '0.0', 'GlobalSearchVolume': '260', 'RegionalSearchVolume': '90', 'LocalSearchTrendsByMonth': {'Nov': '90', 'Oct': '90', 'Sep': '90', 'Aug': '110', 'Jul': '110', 'Jun': '70', 'May': '90', 'Apr': '90', 'Mar': '110', 'Feb': '110', 'Jan': '110', 'Dec': '50'}, 'CPC': '0.0'}, 'KeywordRanking': {'date': '2020-01-05', 'Google': {'Rank': '14', 'BaseRank': '11', 'Url': 'x.com/blog/blindness/'}, 'Bing': {'Rank': '16', 'Url': 'www.x.com/blog/blindness/'}}, 'CreatedAt': '2019-05-01', 'RequestUrl': '/rankings/list?keyword_id=17492833&format=json&from_date=2019-05-01&to_date='},
{'Id': '18540410', 'Keyword': 'research', 'KeywordMarket': 'US-en', 'KeywordLocation': None, 'KeywordDevice': 'desktop', 'KeywordTranslation': None, 'KeywordTags': 'keyword research - 11/19', 'KeywordStats': {'AdvertiserCompetition': '0.00564972', 'GlobalSearchVolume': '320', 'RegionalSearchVolume': '170', 'LocalSearchTrendsByMonth': {'Nov': '170', 'Oct': '170', 'Sep': '210', 'Aug': '260', 'Jul': '140', 'Jun': '140', 'May': '170', 'Apr': '170', 'Mar': '210', 'Feb': '210', 'Jan': '110', 'Dec': '110'}, 'CPC': '0.0'}, 'KeywordRanking': {'date': '2020-01-05', 'Google': {'Rank': '75', 'BaseRank': '73', 'Url': 'x.com/blog/research/'}, 'Bing': {'Rank': '120', 'Url': None}}, 'CreatedAt': '2019-11-01', 'RequestUrl': '/rankings/list?keyword_id=18540410&format=json&from_date=2019-11-01&to_date='}]}}
然后我创建了一个字典
litedict = lite.json()
在这之后我只能看到“回应”
print(litedict["Response"])
如果我尝试提取我想要的数据“result”,我会得到以下错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-69-59a1c50ef855> in <module>
----> 1 print(lite2["Result"])
KeyError: 'Result'
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在里面
---->1次打印(lite2[“结果”])
KeyError:“结果”
任何帮助都将不胜感激,我对JSON非常不熟悉,我
“我已经搜索了大约一个小时的答案。要访问孩子,您需要升级 示例
litedict[“响应”][“结果”][0]
你会得到
{'Id':'18540409','Keyword':blindness','KeywordMarket':'US en','KeywordLocation':None','KeywordDevice':'desktop','KeywordTranslation':None','KeywordTags':'banner blindness-10/18','KeywordStats':{'广告商竞争':'0.0','GlobalSearchVolume':'260','RegionalSearchVolume':'90','LocalTrendsBymonth':{11月:'90',10月:'90',9月:'90',8月:'110',7月:'110',6月:'70',5月:'90',4月:'90',3月:'110',2月:'110',2月:'110',12月:'50',CPC':'0.0',关键字排名:{日期:'2020-01-05',谷歌:{'Rank':'14','BaseRank':'11','Url':'x.com/blog/blindness/'Bing''{'Rank':'16','Url':'www.x.com/blog/blindness/'},'CreatedAt':'2019-05-01','RequestUrl':'/rankings/list?keyword_id=17492833&format=json&from_date=2019-05-01&to_date='}
如果您对响应的格式更加统一,问题就会更加清楚:
{
'Response': {
'responsecode': '200',
'resultsreturned': '100',
'totalresults': '1021',
'nextpage': '/keywords/list?site_id=000000&start=100&format=json',
'Result': [
{...},
{...},
]
}
}
Result
不是顶级对象的键,而是Response
值的键。使用lite['Response']['Result']更仔细地查看JSON:Result
是'Response'
值的键,而不是顶级对象。litedict['Response'][/Result']
是您想要的表达式。我对JSON非常不熟悉。在这种情况下,您可能需要的是一个指南/教程和文档。您有推荐吗?非常好,谢谢。您有推荐一个好的教程的建议吗?