簡體   English   中英

從 Json 中提取

[英]Extracting from Json

從我低於輸出的方法之一

        {'Records': [{'messageId': '2953dfd5-d848-42b2-a60b-43df00ec8e5f', 
       'receiptHandle': 'AQEBPMr5RbW3T2DG4pAYi+', 'body': 
      'I am still trying', 'attributes': {'ApproximateReceiveCount': '1', 
        'SentTimestamp': '1552073955807', 'SenderId': '944198216610', 
        'ApproximateFirstReceiveTimestamp': '1552073955816'}, 
        'messageAttributes': {}, 'md5OfBody': 
         '2111a742ddbdac2d862fa6a204f7dc85', 'eventSource': 'aws:sqs', 
          'eventSourceARN': 'arn:aws:sqs:us-east- 
         1:944198216610:LambadaQueue', 'awsRegion': 'us-east-1'}]}

現在我想從中獲取 body 的值,所以我在下面使用了

body=event['Records'][0][0]['body']

但這不起作用。你能幫我弄清楚我在做什么錯嗎?

我究竟做錯了什么?

Records鍵是一個列表,您可以使用該項目的索引號從列表中選擇項目。

json_string = {
              "Records": [
    {
      "messageId": "2953dfd5-d848-42b2-a60b-43df00ec8e5f",
      "receiptHandle": "AQEBPMr5RbW3T2DG4pAYi+",
      "body": "I am still trying",
      "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "1552073955807",
        "SenderId": "944198216610",
        "ApproximateFirstReceiveTimestamp": "1552073955816"
      },
      "messageAttributes": { },
      "md5OfBody": "2111a742ddbdac2d862fa6a204f7dc85",
      "eventSource": "aws:sqs",
      "eventSourceARN": "arn:aws:sqs:us-east-1:944198216610: LambadaQueue",
      "awsRegion": "us-east-1"
    }
  ]
}

因此,當您執行json_string['Records'][0] ,這將選擇列表中的第一項,它也是字典:

{
  "messageId": "2953dfd5-d848-42b2-a60b-43df00ec8e5f",
  "receiptHandle": "AQEBPMr5RbW3T2DG4pAYi+",
  "body": "I am still trying",
 ....}

現在,如果您執行json_string['Records'][0][0] ,則嘗試訪問字典鍵,例如列表中的項(使用索引號0),這在語法上是不正確的。 如果要訪問'messageId'的值,則可以按名稱訪問鍵,例如json_string['Records'][0]['messageId'] ,或者在您的問題中,像這樣輸入“ body”鍵的值:

`json_string['Records'][0]['body']`

 #Output:
 I am still trying

您是否要獲得“我還在嘗試”?

json_data = {
  'Records': [{
    'messageId': '2953dfd5-d848-42b2-a60b-43df00ec8e5f',
    'receiptHandle': 'AQEBPMr5RbW3T2DG4pAYi+',
    'body': 'I am still trying',
    'attributes': {
        'ApproximateReceiveCount': '1',
        'SentTimestamp': '1552073955807',
        'SenderId': '944198216610',
        'ApproximateFirstReceiveTimestamp': '1552073955816'
    },
    'messageAttributes': {},
    'md5OfBody': '2111a742ddbdac2d862fa6a204f7dc85',
    'eventSource': 'aws:sqs',
    'eventSourceARN': 'arn:aws:sqs:us-east-1: 944198216610: LambadaQueue',
    'awsRegion': 'us - east - 1'
  }]
}

print (json_data['Records'][0]['body'])
# output
# I am still trying

如果您嘗試獲取“ body”元素的值,則似乎應該跳過查找中的第二個[0] 格式正確,如下所示:

{
  "Records": [
    {
      "messageId": "2953dfd5-d848-42b2-a60b-43df00ec8e5f",
      "receiptHandle": "AQEBPMr5RbW3T2DG4pAYi+",
      "body": "I am still trying",
      "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "1552073955807",
        "SenderId": "944198216610",
        "ApproximateFirstReceiveTimestamp": "1552073955816"
      },
      "messageAttributes": { },
      "md5OfBody": "2111a742ddbdac2d862fa6a204f7dc85",
      "eventSource": "aws:sqs",
      "eventSourceARN": "arn:aws:sqs:us-east-1:944198216610: LambadaQueue",
      "awsRegion": "us-east-1"
    }
  ]
}

因此,要獲取“記錄”中第一條記錄的字段“正文”的值,您應該這樣做: body=event['Records'][0]['body']

暫無
暫無

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

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