json - 在响应对象中序列化日期时间对象(Python Rest API)
问题描述
我想在 Lambda 中序列化以下数据并返回它。这里的问题是这个 JSON 有一个 datetime 对象。有人可以帮我序列化这个 JSON,以便我可以将它返回给我的 REST API。
注意:这是来自 boto3.client.describe_instances 的完整响应(针对一个实例过滤)
{
'Reservations': [
{
'Groups': [
],
'Instances': [
{
'AmiLaunchIndex': 0,
'ImageId': '<ami>',
'InstanceId': '<instance_id>',
'InstanceType': 't2.micro',
'KeyName': '<MyKeyPair>',
'LaunchTime': datetime.datetime(2021,
4,
25,
15,
41,
54,
tzinfo=tzlocal()),
'Monitoring': {
'State': 'disabled'
},
'Placement': {
'AvailabilityZone': 'ap-south-1a',
'GroupName': '',
'Tenancy': 'default'
},
'PrivateDnsName': 'ip-172-31-44-178.ap-south-1.compute.internal',
'PrivateIpAddress': '172.31.44.178',
'ProductCodes': [
],
'PublicDnsName': 'ec2-13-234-232-68.ap-south-1.compute.amazonaws.com',
'PublicIpAddress': '13.234.232.68',
'State': {
'Code': 16,
'Name': 'running'
},
'StateTransitionReason': '',
'SubnetId': 'subnet-cb5bb8a0',
'VpcId': 'vpc-6cbf7a07',
'Architecture': 'x86_64',
'BlockDeviceMappings': [
{
'DeviceName': '/dev/sda1',
'Ebs': {
'AttachTime': datetime.datetime(2021,
4,
25,
15,
41,
54,
tzinfo=tzlocal()),
'DeleteOnTermination': True,
'Status': 'attached',
'VolumeId': 'vol-0a7f3ae04c35a5b09'
}
}
],
'ClientToken': '20d1a714-5f5c-46aa-b4c7-65a917bcea4a',
'EbsOptimized': False,
'EnaSupport': True,
'Hypervisor': 'xen',
'NetworkInterfaces': [
{
'Association': {
'IpOwnerId': 'amazon',
'PublicDnsName': 'ec2-13-234-232-68.ap-south-1.compute.amazonaws.com',
'PublicIp': '13.234.232.68'
},
'Attachment': {
'AttachTime': datetime.datetime(2021,
4,
25,
15,
41,
54,
tzinfo=tzlocal()),
'AttachmentId': 'eni-attach-007365005fa2592f2',
'DeleteOnTermination': True,
'DeviceIndex': 0,
'Status': 'attached',
'NetworkCardIndex': 0
},
'Description': '',
'Groups': [
{
'GroupName': 'default',
'GroupId': 'sg-d23193a8'
}
],
'Ipv6Addresses': [
],
'MacAddress': '02:ee:8b:8d:8a:a2',
'NetworkInterfaceId': 'eni-05bf048dfa0537c09',
'OwnerId': '372401663154',
'PrivateDnsName': 'ip-172-31-44-178.ap-south-1.compute.internal',
'PrivateIpAddress': '172.31.44.178',
'PrivateIpAddresses': [
{
'Association': {
'IpOwnerId': 'amazon',
'PublicDnsName': 'ec2-13-234-232-68.ap-south-1.compute.amazonaws.com',
'PublicIp': '13.234.232.68'
},
'Primary': True,
'PrivateDnsName': 'ip-172-31-44-178.ap-south-1.compute.internal',
'PrivateIpAddress': '172.31.44.178'
}
],
'SourceDestCheck': True,
'Status': 'in-use',
'SubnetId': 'subnet-cb5bb8a0',
'VpcId': 'vpc-6cbf7a07',
'InterfaceType': 'interface'
}
],
'RootDeviceName': '/dev/sda1',
'RootDeviceType': 'ebs',
'SecurityGroups': [
{
'GroupName': 'default',
'GroupId': 'sg-d23193a8'
}
],
'SourceDestCheck': True,
'VirtualizationType': 'hvm',
'CpuOptions': {
'CoreCount': 1,
'ThreadsPerCore': 1
},
'CapacityReservationSpecification': {
'CapacityReservationPreference': 'open'
},
'HibernationOptions': {
'Configured': False
},
'MetadataOptions': {
'State': 'applied',
'HttpTokens': 'optional',
'HttpPutResponseHopLimit': 1,
'HttpEndpoint': 'enabled'
},
'EnclaveOptions': {
'Enabled': False
}
}
],
'OwnerId': '372401663154',
'ReservationId': 'r-0da3a940be66fbf58'
}
],
'ResponseMetadata': {
'RequestId': '8582a3a7-93d2-467f-a3c9-e810c9c356e1',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'x-amzn-requestid': '8582a3a7-93d2-467f-a3c9-e810c9c356e1',
'cache-control': 'no-cache, no-store',
'strict-transport-security': 'max-age=31536000; includeSubDomains',
'content-type': 'text/xml;charset=UTF-8',
'content-length': '7122',
'vary': 'accept-encoding',
'date': 'Sun, 25 Apr 2021 15:42:19 GMT',
'server': 'AmazonEC2'
},
'RetryAttempts': 0
}
}
解决方案
推荐阅读
- php - mysqli_query 错误在 MAMP 上引发 500 错误
- reactjs - 使用 Django websocket 和 React 添加 Google Speech
- python - 用户如何在 python 中为 justdial 抓取网页时选择城市名称
- c++11 - 如何在 C++ 中将派生类型指针序列化为接口类型对象?
- javascript - 错误:无法读取未定义的 REACT JS 的属性“地图”
- javascript - 未捕获的 SyntaxError:main.js 中的令牌无效或意外
- flutter - dartdoc 未找到/不工作。飞镖安装坏了?
- python - psutil 工作负载不一致
- mysql - 选项文件的Mysql位置
- google-apps-script - Problems to get only the value of a row within a 2D array Google Apps Script