【问题标题】:How to convert x-www-form-urlencoded to json with Python (Django)如何使用 Python (Django) 将 x-www-form-urlencoded 转换为 json
【发布时间】:2021-06-02 18:36:01
【问题描述】:

我从 api 接收 x-www-form-urlencoded 数据。

data = 'leads%5Bstatus%5D%5B0%5D%5Bid%5D=29078079'

当我尝试用urllib.parse.parse_qsurllib.parse.unquote 转换它时 我有一个像

{
    "leads[status][0][id]": [
        "29078079"
    ]
}

如何将其转换为普通的 json 或 dict ? 像这样

{
    "leads": [
       "id": "29078079"
    ]
}

【问题讨论】:

    标签: python json django x-www-form-urlencoded


    【解决方案1】:

    正确解析的字符串。所以问题不在于如何获得“正常”的 dict,而是如何更改 dict 键:

    dictionary = { "leads[status][0][id]": [ "29078079" ] }
    dictionary['leads'] = dictionary.pop(list(dictionary)[0])
    

    您也可以手动创建字典:

    dictionary =  {data[:5]:[data.split('=')[-1]]}
    

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 2015-08-16
      • 2019-12-04
      • 2020-10-06
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 2021-06-15
      • 2020-08-21
      相关资源
      最近更新 更多