当键值对深度嵌套时,如何从Python的Json可读摘录中返回特定的键值对?

问题描述 投票:0回答:2

我想从此JSON提取物中仅返回'publishedAt'和'textOriginal'键及其对应的值:

{
  "items": [
    {
      "etag": "TqGO73Ytj_bpySwt6jCD8RAnUro",
      "id": "UgzgvW_JT_4Kug4UXWJ4AaABAg",
      "kind": "youtube#commentThread",
      "snippet": {
        "canReply": true,
        "channelId": "UCBJycsmduvYEL83R_U4JriQ",
        "isPublic": true,
        "topLevelComment": {
          "etag": "n7jYB-lAHmIJG-ipDrm9FelJT4A",
          "id": "UgzgvW_JT_4Kug4UXWJ4AaABAg",
          "kind": "youtube#comment",
          "snippet": {
            "authorChannelId": {
              "value": "UC8uGHu-pBSxlvzFbJGtXi_A"
            },
            "authorChannelUrl": "http://www.youtube.com/channel/UC8uGHu-pBSxlvzFbJGtXi_A",
            "authorDisplayName": "20K SUBSCRIBERS WITH 0 VIDEOS CHALLENGE",
            "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJz02tLZkXAUE7sEtLjsTDEwekol_vHryOGCCw=s48-c-k-c0xffffffff-no-rj-mo",
            "canRate": true,
            "channelId": "UCBJycsmduvYEL83R_U4JriQ",
            "likeCount": 1,
            "publishedAt": "2020-06-06T09:36:04Z",
            "textDisplay": "Nobody:<br /><br />Marques Brownlee: <b>Making everyones quarantines better</b>",
            "textOriginal": "Nobody:\n\nMarques Brownlee: *Making everyones quarantines better*",
            "updatedAt": "2020-06-06T09:36:04Z",
            "videoId": "o-_WXXVye3Y",
            "viewerRating": "none"
          }
        },
        "totalReplyCount": 0,
        "videoId": "o-_WXXVye3Y"
      }
    },
    {
      "etag": "ODEKntEIgLndsnBMMj2vs0RUKPA",
      "id": "Ugz6iswc5a4XlepgddR4AaABAg",
      "kind": "youtube#commentThread",
      "snippet": {
        "canReply": true,
        "channelId": "UCBJycsmduvYEL83R_U4JriQ",
        "isPublic": true,
        "topLevelComment": {
          "etag": "akYunoedzz7BEc8TfojnnPF1H2s",
          "id": "Ugz6iswc5a4XlepgddR4AaABAg",
          "kind": "youtube#comment",
          "snippet": {
            "authorChannelId": {
              "value": "UCqw0-jR0Koo3M_XSJmrxNfA"
            },
            "authorChannelUrl": "http://www.youtube.com/channel/UCqw0-jR0Koo3M_XSJmrxNfA",
            "authorDisplayName": "James Beesley",
            "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJz8LxrdmpXBYaiEEaco2ryN75nKnSpoMF0ZnA=s48-c-k-c0xffffffff-no-rj-mo",
            "canRate": true,
            "channelId": "UCBJycsmduvYEL83R_U4JriQ",
            "likeCount": 0,
            "publishedAt": "2020-06-06T09:36:03Z",
            "textDisplay": "God bless America and all the people there, no matter whether white or black. <br />Greetings from an American who living in Germany for years now.",
            "textOriginal": "God bless America and all the people there, no matter whether white or black. \nGreetings from an American who living in Germany for years now.",
            "updatedAt": "2020-06-06T09:36:03Z",
            "videoId": "o-_WXXVye3Y",
            "viewerRating": "none"
          }
        },
        "totalReplyCount": 0,
        "videoId": "o-_WXXVye3Y"
      }
    },
    {
      "etag": "aVFF8Fke4FmEF42qeDm1WmxnoN0",
      "id": "UgwmHDYufIDNBuqvbnt4AaABAg",
      "kind": "youtube#commentThread",
      "snippet": {
        "canReply": true,
        "channelId": "UCBJycsmduvYEL83R_U4JriQ",
        "isPublic": true,
        "topLevelComment": {
          "etag": "6L2tkvTWVmUsAkm2iP3iBJMbans",
          "id": "UgwmHDYufIDNBuqvbnt4AaABAg",
          "kind": "youtube#comment",
          "snippet": {
            "authorChannelId": {
              "value": "UCVYLjLqnqJ8j9ZGb7qQx80Q"
            },
            "authorChannelUrl": "http://www.youtube.com/channel/UCVYLjLqnqJ8j9ZGb7qQx80Q",
            "authorDisplayName": "sgurung18",
            "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJwkCqwDrgE8COyarsavX6avm7pLAgfnGv8yiw=s48-c-k-c0xffffffff-no-rj-mo",
            "canRate": true,
            "channelId": "UCBJycsmduvYEL83R_U4JriQ",
            "likeCount": 0,
            "publishedAt": "2020-06-06T09:35:16Z",
            "textDisplay": "Awesome to hear you speak up!!!",
            "textOriginal": "Awesome to hear you speak up!!!",
            "updatedAt": "2020-06-06T09:35:16Z",
            "videoId": "o-_WXXVye3Y",
            "viewerRating": "none"
          }
        },
        "totalReplyCount": 0,
        "videoId": "o-_WXXVye3Y"
      }
    },
    {
      "etag": "M5rTFFe8oMd3R-h6xa59hXeY3eQ",
      "id": "UgwKJ_x0bslWsBZ-GQJ4AaABAg",
      "kind": "youtube#commentThread",
      "snippet": {
        "canReply": true,
        "channelId": "UCBJycsmduvYEL83R_U4JriQ",
        "isPublic": true,
        "topLevelComment": {
          "etag": "WflvYd0l7vGiR8RRN6eH5BCBhSw",
          "id": "UgwKJ_x0bslWsBZ-GQJ4AaABAg",
          "kind": "youtube#comment",
          "snippet": {
            "authorChannelId": {
              "value": "UCV9QBJiUgeixQ8Xd2y4f2Pg"
            },
            "authorChannelUrl": "http://www.youtube.com/channel/UCV9QBJiUgeixQ8Xd2y4f2Pg",
            "authorDisplayName": "Jonathan Bevington",
            "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJxBdXHr6iTUgmJ9V4h0qJJz3cx6wiyJ9g61iA=s48-c-k-c0xffffffff-no-rj-mo",
            "canRate": true,
            "channelId": "UCBJycsmduvYEL83R_U4JriQ",
            "likeCount": 0,
            "publishedAt": "2020-06-06T09:35:09Z",
            "textDisplay": "We hear you.",
            "textOriginal": "We hear you.",
            "updatedAt": "2020-06-06T09:35:09Z",
            "videoId": "o-_WXXVye3Y",
            "viewerRating": "none"
          }
        },
        "totalReplyCount": 0,
        "videoId": "o-_WXXVye3Y"
      }
    },
    {
      "etag": "WXqPZ5ucjEqAL56qKfg23_8R1YE",
      "id": "Ugx0HIMaJy6cVBq8aid4AaABAg",
      "kind": "youtube#commentThread",
      "snippet": {
        "canReply": true,
        "channelId": "UCBJycsmduvYEL83R_U4JriQ",
        "isPublic": true,
        "topLevelComment": {
          "etag": "7UAPxJd4m6bGmEfwyID9jqSN9x0",
          "id": "Ugx0HIMaJy6cVBq8aid4AaABAg",
          "kind": "youtube#comment",
          "snippet": {
            "authorChannelId": {
              "value": "UCVR6OLvI3hNM4JG2n2VxHjg"
            },
            "authorChannelUrl": "http://www.youtube.com/channel/UCVR6OLvI3hNM4JG2n2VxHjg",
            "authorDisplayName": "Richie M",
            "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJyV3S82luSHN3AJQGi4xdeDDMjzYuSYlssjqg=s48-c-k-c0xffffffff-no-rj-mo",
            "canRate": true,
            "channelId": "UCBJycsmduvYEL83R_U4JriQ",
            "likeCount": 0,
            "publishedAt": "2020-06-06T09:34:28Z",
            "textDisplay": "Theres Nothing wrong to be Proud of being black or white or Asian and Any other race for that matter....If it makes people smile and better/more competitive in a good way then so be it.But when people get hurt or even Die:( That is truely out or order....Times Need to change because we all Bleed Red....And even if we didn&#39;t all bleed red we Shouldn&#39;t be Hurting Them period!! We All Need to do a little soul searching and learn to value others and Life in general No Matter the Differences And thats including The Beautiful Animals on the land and seas.",
            "textOriginal": "Theres Nothing wrong to be Proud of being black or white or Asian and Any other race for that matter....If it makes people smile and better/more competitive in a good way then so be it.But when people get hurt or even Die:( That is truely out or order....Times Need to change because we all Bleed Red....And even if we didn't all bleed red we Shouldn't be Hurting Them period!! We All Need to do a little soul searching and learn to value others and Life in general No Matter the Differences And thats including The Beautiful Animals on the land and seas.",
            "updatedAt": "2020-06-06T09:34:28Z",
            "videoId": "o-_WXXVye3Y",
            "viewerRating": "none"
          }
        },
        "totalReplyCount": 0,
        "videoId": "o-_WXXVye3Y"
      }
    },
    {
      "etag": "MPA9crdN-BqLIwSPpBBiypReLbc",
      "id": "UgyGF-xTpQW0oSTgzNx4AaABAg",
      "kind": "youtube#commentThread",
      "snippet": {
        "canReply": true,
        "channelId": "UCBJycsmduvYEL83R_U4JriQ",
        "isPublic": true,
        "topLevelComment": {
          "etag": "NIQnOOSBIzO3YNqyveVFWkIXiJU",
          "id": "UgyGF-xTpQW0oSTgzNx4AaABAg",
          "kind": "youtube#comment",
          "snippet": {
            "authorChannelId": {
              "value": "UCozpTdS_0gwux3elr9-Fa9g"
            },
            "authorChannelUrl": "http://www.youtube.com/channel/UCozpTdS_0gwux3elr9-Fa9g",
            "authorDisplayName": "Gitesh Dubal",
            "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJxoBeh-Bjt9FGhAso37rsXpgXWqG3HpbWXncQ=s48-c-k-c0xffffffff-no-rj-mo",
            "canRate": true,
            "channelId": "UCBJycsmduvYEL83R_U4JriQ",
            "likeCount": 0,
            "publishedAt": "2020-06-06T09:32:35Z",
            "textDisplay": "Would love to see your parents in video..\ud83d\ude07",
            "textOriginal": "Would love to see your parents in video..\ud83d\ude07",
            "updatedAt": "2020-06-06T09:32:35Z",
            "videoId": "o-_WXXVye3Y",
            "viewerRating": "none"
          }
        },
        "totalReplyCount": 0,
        "videoId": "o-_WXXVye3Y"
      }
    },

[当我感兴趣的键值对嵌套在项目中包含的列表内的topLevelComment内的代码段内时,我会怎么做? (如果我错了,请原谅我。)

感谢您的光临!

python json dictionary key-value
2个回答
1
投票

您要做的第一件事就是解析json:


-1
投票

您可以使用jmespath;它使您可以轻松地遍历嵌套的json文件。

© www.soinside.com 2019 - 2024. All rights reserved.