将嵌套查询字符串请求转换为字典

问题描述 投票:0回答:1

在我看来,将查询字符串数据转换为格式良好的字典时遇到一些困难。

这是我的看法

class VendorPayloadLayerView(generics.GenericAPIView):
    permission_classes = (permissions.AllowAny,)

    def get(self, request, *args, **kwargs):
        print("Here's the request *****")
        print(request)
        payload = request.GET
        print("Here's the decoded queryDict data")
        print(payload)
        
        data = payload.dict()
        print("Here's the dictionary")
        print(data)

这是对视图的请求:

<rest_framework.request.Request: GET '/turnalerts/api/v2/vendor?%7B%22_vnd%22:%20%7B%22v1%22:%20%7B%22author%22:%20%7B%22id%22:%20%22d2e805b5-4a25-4102-a629-e6b67c798ad6%22,%20%22name%22:%20%22WhatsApp%20Business%20Cloud%20API%22,%20%22request_id%22:%20%22GBV3LTlUEUtfjuMHaDYi%22,%20%22type%22:%20%22SYSTEM%22%7D,%20%22card_uuid%22:%20null,%20%22chat%22:%20%7B%22assigned_to%22:%20%7B%22id%22:%20%2278c711b6-2673-cd8b-0fd9-9a6f03bbcdc5%22,%20%22name%22:%20%22Chima%20Chinda%22,%20%22type%22:%20%22OPERATOR%22%7D,%20%22contact_uuid%22:%20%225ba732cf-d424-4163-9d73-98680d4f53f9%22,%20%22inserted_at%22:%20%222022-05-10T10:15:38.808899Z%22,%20%22owner%22:%20%22%202349039756628%22,%20%22permalink%22:%20%22https://whatsapp-praekelt-cloud.turn.io/app/c/ebd12728-e787-4f29-b938-1059b67f4abd%22,%20%22state%22:%20%22OPEN%22,%20%22state_reason%22:%20%22Re-opened%20by%20inbound%20message.%22,%20%22unread_count%22:%2018,%20%22updated_at%22:%20%222024-12-28T22:17:48.825870Z%22,%20%22uuid%22:%20%22ebd12728-e787-4f29-b938-1059b67f4abd%22%7D,%20%22direction%22:%20%22outbound%22,%20%22faq_uuid%22:%20null,%20%22in_reply_to%22:%20null,%20%22inserted_at%22:%20%222024-12-28T22:17:48.817259Z%22,%20%22labels%22:%20[],%20%22last_status%22:%20null,%20%22last_status_timestamp%22:%20null,%20%22on_fallback_channel%22:%20false,%20%22rendered_content%22:%20null,%20%22uuid%22:%20%227d5fc64e-fd77-325f-8a50-6475e4496775%22%7D%7D,%20%22from%22:%20%2227726968450%22,%20%22id%22:%20%22wamid.HBgNMjM0OTAzOTc1NjYyOBUCABEYEjdCMTJFNUZDNzNFQjkxQ0IyRQA%27:%20%27%22,%20%22preview_url%22:%20false,%20%22recipient_type%22:%20%22individual%22,%20%22text%22:%20%7B%22body%22:%20%22The%20MomConnect%20ADA%20Symptom%20Checker%20is%20unfortunately%20no%20longer%20available.%20%5C%5Cn%5C%5CnPlease%20reply%20*ASK*%20if%20you%20have%20questions%20or%20need%20help.%22%7D,%20%22timestamp%22:%20%221735424268%22,%20%22to%22:%20%222349039756628%22,%20%22type%22:%20%22text%22%7D'>

这是使用 request.GET 解码后的 queryDict

<QueryDict: {'{"_vnd": {"v1": {"author": {"id": "d2e805b5-4a25-4102-a629-e6b67c798ad6", "name": "WhatsApp Business Cloud API", "request_id": "GBV3LTlUEUtfjuMHaDYi", "type": "SYSTEM"}, "card_uuid": null, "chat": {"assigned_to": {"id": "78c711b6-2673-cd8b-0fd9-9a6f03bbcdc5", "name": "Chima Chinda", "type": "OPERATOR"}, "contact_uuid": "5ba732cf-d424-4163-9d73-98680d4f53f9", "inserted_at": "2022-05-10T10:15:38.808899Z", "owner": " 2349039756628", "permalink": "https://whatsapp-praekelt-cloud.turn.io/app/c/ebd12728-e787-4f29-b938-1059b67f4abd", "state": "OPEN", "state_reason": "Re-opened by inbound message.", "unread_count": 18, "updated_at": "2024-12-28T22:17:48.825870Z", "uuid": "ebd12728-e787-4f29-b938-1059b67f4abd"}, "direction": "outbound", "faq_uuid": null, "in_reply_to": null, "inserted_at": "2024-12-28T22:17:48.817259Z", "labels": [], "last_status": null, "last_status_timestamp": null, "on_fallback_channel": false, "rendered_content": null, "uuid": "7d5fc64e-fd77-325f-8a50-6475e4496775"}}, "from": "27726968450", "id": "wamid.HBgNMjM0OTAzOTc1NjYyOBUCABEYEjdCMTJFNUZDNzNFQjkxQ0IyRQA\': \'", "preview_url": false, "recipient_type": "individual", "text": {"body": "The MomConnect ADA Symptom Checker is unfortunately no longer available. \\\\n\\\\nPlease reply *ASK* if you have questions or need help."}, "timestamp": "1735424268", "to": "2349039756628", "type": "text"}': ['']}>

最后,这是字典作为payload.dict()

Here's the dictionary
{'{"_vnd": {"v1": {"author": {"id": "d2e805b5-4a25-4102-a629-e6b67c798ad6", "name": "WhatsApp Business Cloud API", "request_id": "GBV3LTlUEUtfjuMHaDYi", "type": "SYSTEM"}, "card_uuid": null, "chat": {"assigned_to": {"id": "78c711b6-2673-cd8b-0fd9-9a6f03bbcdc5", "name": "Chima Chinda", "type": "OPERATOR"}, "contact_uuid": "5ba732cf-d424-4163-9d73-98680d4f53f9", "inserted_at": "2022-05-10T10:15:38.808899Z", "owner": " 2349039756628", "permalink": "https://whatsapp-praekelt-cloud.turn.io/app/c/ebd12728-e787-4f29-b938-1059b67f4abd", "state": "OPEN", "state_reason": "Re-opened by inbound message.", "unread_count": 18, "updated_at": "2024-12-28T22:17:48.825870Z", "uuid": "ebd12728-e787-4f29-b938-1059b67f4abd"}, "direction": "outbound", "faq_uuid": null, "in_reply_to": null, "inserted_at": "2024-12-28T22:17:48.817259Z", "labels": [], "last_status": null, "last_status_timestamp": null, "on_fallback_channel": false, "rendered_content": null, "uuid": "7d5fc64e-fd77-325f-8a50-6475e4496775"}}, "from": "27726968450", "id": "wamid.HBgNMjM0OTAzOTc1NjYyOBUCABEYEjdCMTJFNUZDNzNFQjkxQ0IyRQA\': \'", "preview_url": false, "recipient_type": "individual", "text": {"body": "The MomConnect ADA Symptom Checker is unfortunately no longer available. \\\\n\\\\nPlease reply *ASK* if you have questions or need help."}, "timestamp": "1735424268", "to": "2349039756628", "type": "text"}': ''}

这里的问题是,最终结果不是有效的 json,因为它有一个额外的大括号,字典周围有一个单引号。

我想要得到什么:

{"_vnd": {"v1": {"author": {"id": "d2e805b5-4a25-4102-a629-e6b67c798ad6", "name": "WhatsApp Business Cloud API", "request_id": "GBV3LTlUEUtfjuMHaDYi", "type": "SYSTEM"}, "card_uuid": null, "chat": {"assigned_to": {"id": "78c711b6-2673-cd8b-0fd9-9a6f03bbcdc5", "name": "Chima Chinda", "type": "OPERATOR"}, "contact_uuid": "5ba732cf-d424-4163-9d73-98680d4f53f9", "inserted_at": "2022-05-10T10:15:38.808899Z", "owner": " 2349039756628", "permalink": "https://whatsapp-praekelt-cloud.turn.io/app/c/ebd12728-e787-4f29-b938-1059b67f4abd", "state": "OPEN", "state_reason": "Re-opened by inbound message.", "unread_count": 18, "updated_at": "2024-12-28T22:17:48.825870Z", "uuid": "ebd12728-e787-4f29-b938-1059b67f4abd"}, "direction": "outbound", "faq_uuid": null, "in_reply_to": null, "inserted_at": "2024-12-28T22:17:48.817259Z", "labels": [], "last_status": null, "last_status_timestamp": null, "on_fallback_channel": false, "rendered_content": null, "uuid": "7d5fc64e-fd77-325f-8a50-6475e4496775"}}, "from": "27726968450", "id": "wamid.HBgNMjM0OTAzOTc1NjYyOBUCABEYEjdCMTJFNUZDNzNFQjkxQ0IyRQA\': \'", "preview_url": false, "recipient_type": "individual", "text": {"body": "The MomConnect ADA Symptom Checker is unfortunately no longer available. \\\\n\\\\nPlease reply *ASK* if you have questions or need help."}, "timestamp": "1735424268", "to": "2349039756628", "type": "text"}

额外的字符(数据开头和结尾处的大括号和单逗号)不会出现在查询字符串中,因此我不清楚是什么创建了它。

谢谢。

python json django dictionary
1个回答
0
投票

这是因为查询字符串只是一个“一级”字典:它生成字符串键和字符串值。该值看起来像 JSON blob,这一事实没有多大意义。

这里甚至让情况变得更糟,因为您在查询字符串中使用了 one 键,该键映射到 no 值。这里的关键是一个“丑陋”的 JSON blob。您可以通过尝试将键解析为 JSON blob 来实现此功能,例如: import json result = {} for ky in request.GET: try: ky = json.loads(ky) if isinstance(ky, dict): result.update(ky) except ValueError: pass

但它仍然是
丑陋

。如果将 JSON blob 作为 QueryString 传递,请针对固定键执行此操作,然后 JSON 解码该特定键。在这里,您实际上是在滥用 URL 的查询字符串部分。

© www.soinside.com 2019 - 2024. All rights reserved.