首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python如何遍历嵌套的json字典并只打印选定的值

Python如何遍历嵌套的json字典并只打印选定的值
EN

Stack Overflow用户
提问于 2022-08-26 23:43:46
回答 1查看 35关注 0票数 0

在这个包含10个JSON的字典中,我只想打印文本、标题、url和TextualRating。我有点困惑,因为嵌套的字典。

我如何循环所有这些,并只打印选定的参数我们为他们全部(文本,标题,url和TextualRating)

Json查看者照片:这是Json美丽的照片

Json代码:

{“声明”:“苹果AirPods破坏你的健康,发射的辐射是耳机的5倍,辐射比微波炉多”,“索赔人”:“社交媒体用户”,“声明日期”:‘2022-05-01T00:00:00:00Z’,‘评论’:{'publisher':{'name':‘美国之今’,'usatoday.com'} },‘'url':'https://www.usatoday.com/story/news/factcheck/2022/08/10/fact-check-context-missing-claim-health-effects-airpods/9661778002/','title':’事实检查:声称AirPods和其他蓝牙耳机的健康影响‘,'reviewDate':'2022-08-11T01:33:14Z',’文本分析‘:’缺失上下文‘,’语言代码‘:'en'}},{'text':’‘,“索赔人”:“脸book.com”,“声明”:“2022-07-01T20:40:00Z”,“声明评论”:{“出版商”:{“名称”:“铅故事”,“站点”:“leadstories.com”},“url”:'https://leadstories.com/hoax-alert/2022/07/fact-check-apple-logo-is-not-honoring-inventor-alan-turing-who-may-have-died-by-poisoned-apple.html',“标题”:“事实查证:苹果Logo没有表彰可能死于苹果中毒的发明者艾伦图灵”、“评论日期”:“2022-07-01T20:40:00Z”、“文本分析”:“城市传说”、“语言代码”:“en”}、{“文本”:“来自苹果AirPods危险的辐射”、“申索人”:“社交媒体用户”、“声明评论”:{ {'publisher':{'name':‘thip.media’、‘thip.media’}、'thip.media'},‘'url':'https://www.thip.media/health-news-fact-check/fact-check-is-radiation-from-apple-airpods-dangerous/30863/','title':’事实检查:来自苹果AirPods的辐射危险吗?‘、'reviewDate':’2022-0504T00:00:00Z‘、’大部分错误‘、’语言代码‘:’en‘}}、'nextPageToken':’CAM‘}’‘。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-26 23:57:31

我希望你的json结构如下,

代码语言:javascript
运行
复制
{'claims': [{
                       'text': 'Apple AirPods destroy your health, emit 5 times more radiation than over-ear headphones and emit "more radiation than a microwave"',
                       'claimant': 'Social media users', 'claimDate': '2022-05-01T00:00:00Z', 'claimReview': [
        {'publisher': {'name': 'USA Today', 'site': 'usatoday.com'},
         'url': 'https://www.usatoday.com/story/news/factcheck/2022/08/10/fact-check-context-missing-claim-health-effects-airpods/9661778002/',
         'title': 'Fact check: Claim about health effects of AirPods and other Bluetooth earphones is missing context',
         'reviewDate': '2022-08-11T01:33:14Z', 'textualRating': 'Missing Context', 'languageCode': 'en'}]},
                   {'text': 'Apple Logo Alan Turing Death', 'claimant': 'facebook.com',
                    'claimDate': '2022-07-01T20:40:00Z', 'claimReview': [
                       {'publisher': {'name': 'Lead Stories', 'site': 'leadstories.com'},
                        'url': 'https://leadstories.com/hoax-alert/2022/07/fact-check-apple-logo-is-not-honoring-inventor-alan-turing-who-may-have-died-by-poisoned-apple.html',
                        'title': 'Fact Check: Apple Logo Is NOT Honoring Inventor Alan Turing Who May Have Died By Poisoned Apple',
                        'reviewDate': '2022-07-01T20:40:00Z', 'textualRating': 'Urban Legend', 'languageCode': 'en'}]},
                   {'text': 'Radiation from Apple AirPods dangerous', 'claimant': 'Social media user', 'claimReview': [
                       {'publisher': {'name': 'THIP Media', 'site': 'thip.media'},
                        'url': 'https://www.thip.media/health-news-fact-check/fact-check-is-radiation-from-apple-airpods-dangerous/30863/',
                        'title': 'Fact Check: Is radiation from Apple AirPods dangerous?',
                        'reviewDate': '2022-05-04T00:00:00Z', 'textualRating': 'Mostly False', 'languageCode': 'en'}]}],
        'nextPageToken': 'CAM'}

在这种情况下,您可以使用以下代码提取值的形式键,

代码语言:javascript
运行
复制
claims_json_data = {'claims': [{
                       'text': 'Apple AirPods destroy your health, emit 5 times more radiation than over-ear headphones and emit "more radiation than a microwave"',
                       'claimant': 'Social media users', 'claimDate': '2022-05-01T00:00:00Z', 'claimReview': [
        {'publisher': {'name': 'USA Today', 'site': 'usatoday.com'},
         'url': 'https://www.usatoday.com/story/news/factcheck/2022/08/10/fact-check-context-missing-claim-health-effects-airpods/9661778002/',
         'title': 'Fact check: Claim about health effects of AirPods and other Bluetooth earphones is missing context',
         'reviewDate': '2022-08-11T01:33:14Z', 'textualRating': 'Missing Context', 'languageCode': 'en'}]},
                   {'text': 'Apple Logo Alan Turing Death', 'claimant': 'facebook.com',
                    'claimDate': '2022-07-01T20:40:00Z', 'claimReview': [
                       {'publisher': {'name': 'Lead Stories', 'site': 'leadstories.com'},
                        'url': 'https://leadstories.com/hoax-alert/2022/07/fact-check-apple-logo-is-not-honoring-inventor-alan-turing-who-may-have-died-by-poisoned-apple.html',
                        'title': 'Fact Check: Apple Logo Is NOT Honoring Inventor Alan Turing Who May Have Died By Poisoned Apple',
                        'reviewDate': '2022-07-01T20:40:00Z', 'textualRating': 'Urban Legend', 'languageCode': 'en'}]},
                   {'text': 'Radiation from Apple AirPods dangerous', 'claimant': 'Social media user', 'claimReview': [
                       {'publisher': {'name': 'THIP Media', 'site': 'thip.media'},
                        'url': 'https://www.thip.media/health-news-fact-check/fact-check-is-radiation-from-apple-airpods-dangerous/30863/',
                        'title': 'Fact Check: Is radiation from Apple AirPods dangerous?',
                        'reviewDate': '2022-05-04T00:00:00Z', 'textualRating': 'Mostly False', 'languageCode': 'en'}]}],
        'nextPageToken': 'CAM'}

for claim in claims_json_data['claims']:
    print(f"text: {claim['text']}")
    for review in claim['claimReview']:
        print(f"url: {review['url']}")
        print(f"title: {review['title']}")
        print(f"textualRating {review['textualRating']}")
        print('\n')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73507385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档