簡體   English   中英

Python 如何遍歷嵌套的 json 字典並僅打印選定的值

[英]Python How to Iterate through the nested json dictionaries and print only selected value

從這個包含 10 個聲明的 JSON 字典中,我只想打印文本、標題、url 和 TextualRating。 由於嵌套字典,我有點困惑。

我如何遍歷所有這些並僅打印我們為它們選擇的參數(文本、標題、url 和 TextualRating)

照片來自 Json 查看器:這是 Json 美化照片

Json 代碼:

'''{'claims': [{'text': 'Apple AirPods 會破壞你的健康,發出的輻射是頭戴式耳機的 5 倍,並且發出的輻射“比微波爐還多”', 'claimant': '社交媒體用戶', 'claimDate': '2022-05-01T00:00:00Z', 'claimReview': [{'publisher': {'name': 'USA Today', 'site': 'usatoday.com'}, '網址':'https://www.usatoday.com/story/news/factcheck/2022/08/10/fact-check-context-missing-claim-health-effects-airpods/9661778002/','標題': '事實核查:關於 AirPods 和其他藍牙耳機的健康影響的聲明缺少上下文','reviewDate':'2022-08-11T01:33:14Z','textualRating':'缺少上下文','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.html','標題' :“事實核查:Apple 徽標不是為了紀念可能死於蘋果中毒的發明家 Alan Turing”、“reviewDate”:“2022-07-01T20:40:00Z”、“textualRating”:“Urban Legend”、“languageCode” : 'en'}]}, {'text': '來自 Apple AirPods 的輻射很危險', 'claimant': '社交媒體用戶', 'claimReview': [{'publisher': {'name': 'THIP Media' , '網站': 'thip.media'}, 'url': 'https://www.thip.media/health-news-fact-check/fact-check-is-radiation-from-apple-airpods-dangerous /30863/', 'title': 'Fact Check: 來自 Apple AirPods 的輻射危險嗎?', 'reviewDate': '2022-05-04T00:00:00Z', 'textualRating': 'Mostly False', 'languageCode' : 'en'}]}], 'nextPageToken': 'CAM'}'''

我希望你的 json 結構如下,

{'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'}

在這種情況下,您可以使用以下代碼從鍵中提取值,

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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM