当键值对深度嵌套时,如何从 Json 中的 Python 可读提取物中 return 特定键值对?

How to return specific key value pairs from this Json readable extract in Python when key value pair is deeply nested?

我想 return 只是 'publishedAt' 和 'textOriginal' 键及其相应值从此 JSON 提取:

{
  "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"
      }
    },

当我感兴趣的键值对嵌套在 snippet 内 topLevelComment 内 snippet 内 snippet 内 topLevelComment 内 snippet 内包含在项目中时,你会建议我如何处理? (如有错误请见谅)

感谢观看!

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

import json
data = 'your json data here'
parsed_data = json.loads(data)

然后您可以访问您感兴趣的元素:

element = data['first']['second']['third']

其中 first, second, third 是访问嵌套数据的键名。

如果数据是一个列表,那么你可以这样做:

import json
d = 'your json data here'
parsed_data = json.loads(d)
items = parsed_data['items']
for publication_data in items:
    print(publication_data['snippet']['topLevelComment']['snippet']['publishedAt'])
    print(publication_data['snippet']['topLevelComment']['snippet']['textOriginal'])

可以使用jmespath;它允许您很容易地遍历嵌套的 json 文件。

使用要点:如果数据在列表中,您可以使用 [] 符号访问它,如果它在字典中,则可以使用 . 符号。

对于您的数据,顶级键是项目,然后是列表,然后是子字典,然后是 publishedAt 和 textOriginal。所以伪代码:

items->list->dictionary->dictionary->dictionary->our targets

代码如下所示:

import jmespath


  data  = “””[your data above]”””

expression = jmespath.compile("items[*].snippet.topLevelComment.snippet.[{publishedAt:publishedAt, textOriginal:textOriginal}]")

outcome = expression.search(data)


print(outcome)

[[{'publishedAt': '2020-06-06T09:36:04Z',
   'textOriginal': 'Nobody:\n\nMarques Brownlee: *Making everyones quarantines better*'}],
 [{'publishedAt': '2020-06-06T09:36:03Z',
   '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.'}],
 [{'publishedAt': '2020-06-06T09:35:16Z',
   'textOriginal': 'Awesome to hear you speak up!!!'}],
 [{'publishedAt': '2020-06-06T09:35:09Z', 'textOriginal': 'We hear you.'}],
 [{'publishedAt': '2020-06-06T09:34:28Z',
   '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."}],
 [{'publishedAt': '2020-06-06T09:32:35Z',
   'textOriginal': 'Would love to see your parents in video..\ud83d\ude07'}]]