要在AWS Lambda中访问对象中的键,请确保在Lambda函数中传递的对象是一个JSON字符串,然后使用JSON.parse()方法将其转换为JavaScript对象。这样,Lambda函数就可以正确地读取对象中的键值。
以下是一个示例代码,它将JSON对象转换为Lambda函数中的JavaScript对象:
exports.handler = function(event, context, callback) { // Parse the event object as JSON var eventObj = JSON.parse(event);
// Access the 'key1' value from the 'eventObj' var keyValue = eventObj.key1;
// Do something with the keyValue console.log("The 'key1' value is: " + keyValue);
// Return the results callback(null, "Success!"); };
通过这种方法,AWS Lambda可以正确地读取JavaScript对象中的键值。