【问题标题】:How to get value of key from jsondata如何从jsondata中获取key的值
【发布时间】:2018-10-15 14:46:13
【问题描述】:
{
"Records": [{
    "messageId": "20ea364e-3bc107b5c78c",
    "receiptHandle": "AQEB6DhNloFS4R66c=",
    "body": "1",
    "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "15393506",
        "SenderId": "AROAJMTI6NE:errorLog",
        "ApproximateFirstReceiveTimestamp": "15393511"
    },
    "messageAttributes": {},
    "md5OfBody": "c4ca75849b",
    "eventSource": "aws:sqs",
    "eventSourceARN": "arn:aws:sqs:ap-suth-1:83362:escalateErrorStandardQueue",
    "awsRegion": "ap-south-1"
}]

}

我想得到键“body”的值,预期的输出应该是:1

【问题讨论】:

  • 你试过data.Records[0].body吗?其中 data 是分配给此 json 的变量的名称

标签: javascript node.js json lambda amazon-sqs


【解决方案1】:

var data={
"Records": [{
    "messageId": "20ea364e-3bc107b5c78c",
    "receiptHandle": "AQEB6DhNloFS4R66c=",
    "body": "1",
    "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "15393506",
        "SenderId": "AROAJMTI6NE:errorLog",
        "ApproximateFirstReceiveTimestamp": "15393511"
    },
    "messageAttributes": {},
    "md5OfBody": "c4ca75849b",
    "eventSource": "aws:sqs",
    "eventSourceARN": "arn:aws:sqs:ap-suth-1:83362:escalateErrorStandardQueue",
    "awsRegion": "ap-south-1"
}]
};
console.log(data.Records[0].body);

您只需data.Records[0].body即可访问数据。

【讨论】:

    【解决方案2】:

    如果 jsondata 的名称是data(可以这么说):

    data.Records[0].body
    

    【讨论】:

      【解决方案3】:

      试试这个:

      (data && data['Records'] && data['Records'][0] && data['Records'][0]['body']) || 1;
      

      它永远不会给你任何错误,如果存在则返回 key 'body' 值,否则为 1。

      【讨论】:

        【解决方案4】:

        如果您的记录是一个数组,您可以尝试迭代您的Records 并获取正文的值。

        另外,如果您使用布尔条件,最好将值解析为整数,因为来自JSON 的数据将返回字符串(自然);在我的示例中,我使用 + Unary Plus 运算符解析它。

        var data = {
          "Records": [{
              "messageId": "20ea364e-3bc107b5c78c",
              "receiptHandle": "AQEB6DhNloFS4R66c=",
              "body": "1",
              "attributes": {
                  "ApproximateReceiveCount": "1",
                  "SentTimestamp": "15393506",
                  "SenderId": "AROAJMTI6NE:errorLog",
                  "ApproximateFirstReceiveTimestamp": "15393511"
              },
              "messageAttributes": {},
              "md5OfBody": "c4ca75849b",
              "eventSource": "aws:sqs",
              "eventSourceARN": "arn:aws:sqs:ap-suth-1:83362:escalateErrorStandardQueue",
              "awsRegion": "ap-south-1"
          }]
        };
        
        for (record in data.Records)
        {
          console.log('Normal JSON value:', typeof data.Records[record].body);
          console.log('Parsed JSON value:', typeof +data.Records[record].body);
          console.log('The Record body is:', +data.Records[record].body);
        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-03
          • 1970-01-01
          • 2016-07-07
          • 1970-01-01
          相关资源
          最近更新 更多