如何从jsondata中获取key的值
How to get value of key from jsondata
{
"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
如果 jsondata 的名称是 data
(比方说):
data.Records[0].body
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
即可访问数据。
试试这个:
(data && data['Records'] && data['Records'][0] && data['Records'][0]['body']) || 1;
它永远不会给你任何错误和 return key 'body' value if exist otherwise 1.
如果您的记录是一个数组,您可以尝试迭代您的 Records
并获取正文的值。
此外,如果您正在执行布尔条件,最好将值解析为整数,因为来自 JSON
的数据将 return 字符串(自然地);在我的示例中,我使用 +
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);
}
{
"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
如果 jsondata 的名称是 data
(比方说):
data.Records[0].body
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
即可访问数据。
试试这个:
(data && data['Records'] && data['Records'][0] && data['Records'][0]['body']) || 1;
它永远不会给你任何错误和 return key 'body' value if exist otherwise 1.
如果您的记录是一个数组,您可以尝试迭代您的 Records
并获取正文的值。
此外,如果您正在执行布尔条件,最好将值解析为整数,因为来自 JSON
的数据将 return 字符串(自然地);在我的示例中,我使用 +
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);
}