如何从字典中的键值中删除双引号

How to remove double quote from key value in dictionary

我有这个负载:

            "cardType": "CHOICE",
            "step": 40,
            "title": {"en-GB": "YOUR REPORT"},
            "description": {"en-GB": ""},
            "options": [
                {"optionId": 0, "text": {"en-GB": "Ask me"}},
                {"optionId": 1, "text": {"en-GB": "Phone a nurse"}},
                {"optionId": 2, "text": {"en-GB": "Download full report"}},
            ],
            "_links": {
                "self": {
                    "method": "GET",
                    "href": "/assessments/898d915e-229f-48f2-9b98-cfd760ba8965",
                },
                "report": {
                    "method": "GET",
                    "href": "/reports/17340f51604cb35bd2c6b7b9b16f3aec",
                },
            },
        }

然后我 url 像这样编码并重定向到报告视图:

url = reverse("my-reports")
reverse_url = encodeurl(data, url)

url编码输出返回如下:

"/api/v2/ada/reports?cardType=CHOICE&step=40&title="
        "%7B%27en-GB%27%3A+%27YOUR+REPORT%27%7D&description="
        "%7B%27en-GB%27%3A+%27%27%7D&options="
        "%5B%7B%27optionId%27%3A+0%2C+%27text%27%3A+%7B%27en-GB"
        "%27%3A+%27Ask+me%27%7D%7D%2C+%7B%27optionId%27%"
        "3A+1%2C+%27text%27%3A+%7B%27en-GB%27%3A+%27Phone+a+nurse"
        "%27%7D%7D%2C+%7B%27optionId%27%3A+2%2C+%27text%27%3A+%7B"
        "%27en-GB%27%3A+%27Download+full+report%27%7D%7D%5D&_links="
        "%7B%27self%27%3A+%7B%27method%27%3A+%27GET%27%2C+%27href%27%"
        "3A+%27%2Fassessments%2F898d915e-229f-48f2-9b98-cfd760ba8965"
        "%27%7D%2C+%27report%27%3A+%7B%27method%27%3A+%27GET%27%"
        "2C+%27href%27%3A+%27%2Freports%2F"
        "17340f51604cb35bd2c6b7b9b16f3aec%27%7D%7D"

在我的报告视图中,我从 url 查询字符串中获取有效负载:

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

    def get(self, request, *args, **kwargs):
        result = request.GET
        data = result.dict()
        print(data)

现在的问题是上面打印的传输数据的输出包含嵌套键的引号。

{
    'cardType': 'CHOICE', 
    'step': '40', 'title': "{'en-GB': 'YOUR REPORT'}", 
    'description': "{'en-GB': ''}", 
    'options': "[{'optionId': 0, 
    'text': {'en-GB': 'Ask me'}}, 
    {'optionId': 1, 'text': {'en-GB': 'Phone a nurse'}}, {'optionId': 2, 'text': {'en-GB': 'Download full report'}}]", 
    '_links': "{'self': {'method': 'GET', 'href': '/assessments/898d915e-229f-48f2-9b98-cfd760ba8965'}, 'report': {'method': 'GET', 'href': '/reports/17340f51604cb35bd2c6b7b9b16f3aec'}}"
}

注意“描述”、“选项”键等中的双引号。 有没有办法删除它或将它传递给视图,以便当我打开它时,我拥有与开始时相同的数据。

谢谢

import urllib.parse
import json

# convert your dict to a string
json_string = json.dumps(your_payload)

# urlencode the json string
encoded = urllib.parse.quote(json_string.encode('utf-8'))

# build the url
url = reverse("my-reports")
reverse_url = f"{url}?payload={encoded}"

那么在你看来:

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

    def get(self, request, *args, **kwargs):
        payload = request.GET.get('payload')
        
        # do the encoding process in reverse
        urldecoded = urllib.parse.unquote(payload)
        data = json_loads(urldecoded)
        print(data)