从Json中提取

从Json中提取,json,python-3.x,amazon-s3,amazon-sqs,Json,Python 3.x,Amazon S3,Amazon Sqs,从其中一种方法中,我得到了低于输出的结果 {'Records': [{'messageId': '2953dfd5-d848-42b2-a60b-43df00ec8e5f', 'receiptHandle': 'AQEBPMr5RbW3T2DG4pAYi+', 'body': 'I am still trying', 'attributes': {'ApproximateReceiveCount': '1', 'SentTimest

从其中一种方法中,我得到了低于输出的结果

        {'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=事件['Records'][0][0]['body']


但这不起作用。你能帮我找出我做错了什么吗?

如果你试图获取“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"
    }
  ]
}
`json_string['Records'][0]['body']`

 #Output:
 I am still trying
因此,要获取“Records”中第一条记录的字段“body”的值,您应该执行以下操作:
body=event['Records'][0]['body']

我做错了什么

记录
键是一个列表,您可以使用该项目的索引号从列表中选择项目

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\u字符串['Records'][0][0]
,那么您正试图访问一个字典键,比如列表中的一个项目(使用索引号0),这在语法上是不正确的。如果您想访问“messageId”的值,或者像您的问题一样,访问“body”键的值,您可以按名称访问该键,例如
json_string['Records'][0]['messageId']

{
  "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]['body']`

 #Output:
 I am still trying

你想要这个?我仍在尝试你是否尝试过
事件['Records'][0]['body']
?这是一个可靠的答案,因为你花了时间在OP上举例说明。为此,它值得一次投票。谢谢,我自己也曾为JSON做过不少努力。