gpt4 book ai didi

python - 如何检查python中是否存在JSON键/对象

转载 作者:行者123 更新时间:2023-12-05 09:13:48 24 4
gpt4 key购买 nike

我是 python 的新手。除了我的以下代码之外,是否还有检查 python 中是否存在特定 JSON 对象的方法?承认我的以下代码不是好的做法,因此需要知道哪种方法更易于检查和维护?

这是 JSON 响应:

[
{
"MessageId": "250e37a8-d779-48a1-9941-84219a82513e",
"ReceiptHandle": "AQEBjualXe2ywqTgIVmCNI5sKj7r48werf84HHA2BWZimwiEXLFxA/MiPBclK048NZBtOnM3dSDfoiwwqoNTPxTRz+IChd8McziweCxHX6texjAOi/MyAQjCWP+2hJPoxzQgXx9kjnKbepKlcgxhpOiQZe6WiSIq0dXwHHXSA7SP0g9NIR/dU38b+wmo0m2q7MNVfSct967EKF49wow9RHyFMO8iD8fH93PYT9om5NdUha3dvkWnisKcfuO5pZY3LLXPAnuZT/VfqxJjmPqb98iepBfqFb6SpM/02IVSql81XKJEbMBc4zPHp/Uace6e4UDGsn/hPCVsqQsTzrbKCR+ovpkhXipWwTYSlgsLe/o43k0UxhCN8eKhg835KuUkskA3T8C5Q6v6xgznlR7JJuhZpg==",
"MD5OfBody": "bbdc5fdb8be7251f5c910905db994bab",
"Body": "Information about current NY Times fiction bestseller for week of 12/11/2016.",
"Attributes": {
"SentTimestamp": "1553851566164"
},
"MD5OfMessageAttributes": "d25a6aea97eb8f585bfa92d314504a92",
"MessageAttributes": {
"Author": {
"StringValue": "John Grisham",
"DataType": "String"
},
"Title": {
"StringValue": "The Whistler",
"DataType": "String"
},
"WeeksOn": {
"StringValue": "6",
"DataType": "Number"
}
}
}
]

这是我要检查的 python 代码:

if 'Messages' in response:
message = response['Messages'][0]
receipt_handle = message['ReceiptHandle']

sqs.delete_message(
QueueUrl=queue_url,
ReceiptHandle=receipt_handle
)
print('Received and deleted message: %s' % message)
else:
print('Message not received yet')

请让我知道上面的代码是否是好的做法。

最佳答案

您可以检查的另一个选项,也是我在使用 JSON 数据时实际上更喜欢的选项,是从 JSON 数据创建一个对象并使用 hasattr 方法。这将防止您开始过度使用 try-except block ,还可以使您的代码更易于理解。使用您的数据的示例如下:

data= '''
{
"MessageId": "250e37a8-d779-48a1-9941-84219a82513e",
"ReceiptHandle": "AQEBjualXe2ywqTgIVmCNI5sKj7r48werf84HHA2BWZimwiEXLFxA/MiPBclK048NZBtOnM3dSDfoiwwqoNTPxTRz+IChd8McziweCxHX6texjAOi/MyAQjCWP+2hJPoxzQgXx9kjnKbepKlcgxhpOiQZe6WiSIq0dXwHHXSA7SP0g9NIR/dU38b+wmo0m2q7MNVfSct967EKF49wow9RHyFMO8iD8fH93PYT9om5NdUha3dvkWnisKcfuO5pZY3LLXPAnuZT/VfqxJjmPqb98iepBfqFb6SpM/02IVSql81XKJEbMBc4zPHp/Uace6e4UDGsn/hPCVsqQsTzrbKCR+ovpkhXipWwTYSlgsLe/o43k0UxhCN8eKhg835KuUkskA3T8C5Q6v6xgznlR7JJuhZpg==",
"MD5OfBody": "bbdc5fdb8be7251f5c910905db994bab",
"Body": "Information about current NY Times fiction bestseller for week of 12/11/2016.",
"Attributes": {"SentTimestamp": "1553851566164"},
"MD5OfMessageAttributes": "d25a6aea97eb8f585bfa92d314504a92",
"MessageAttributes": {"Author": {"StringValue": "John Grisham","DataType": "String"},"Title": {"StringValue": "The Whistler","DataType": "String"},"WeeksOn": {"StringValue": "6","DataType": "Number"}}
} '''

import json

class Response:

def __init__(self, data):
self.__dict__ = json.loads(data)

response = Response(data)

if hasattr(response , 'MessageId'):
receipt_handle = response.ReceiptHandle
print("Received and deleted message: %s" % response.MessageId)

else:
print('Message not received yet')

输出:

Received and deleted message: 250e37a8-d779-48a1-9941-84219a82513e

关于python - 如何检查python中是否存在JSON键/对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55414516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com