django 如何将请求 json 字符串转换为 object 来处理它

django how to convert request json string to object to process it

我收到来自网页的 json 回复


data=%7B%22header%22%3A%22%7B%5C%22salno%5C%22%3A%5C%220%5C%22%2C%5C%22saldt%5C %22%3A%5C%22Wed+Jul+28+2021+00%3A33%3A40+GMT%2B0530+(印度+标准+时间)%5C%22%2C%5C%22soldto%5C%22%3A%5C% 22%5C%22%2C%5C%22remark%5C%22%3A%5C%22%5C%22%2C%5C%22totqty%5C%22%3A310%2C%5C%22subtot%5C%22%3A20251% 7D%22%2C%22details%22%3A%22%5B%7B%5C%22status%5C%22%3A%5C%22old-modified%5C%22%2C%5C%22saldtlid%5C%22%3A% 5C%221%5C%22%2C%5C%22itemnm%5C%22%3A%5C%22item1%5C%22%2C%5C%22qty%5C%22%3A%5C%2218%5C%22%2C% 5C%22rate%5C%22%3A%5C%2212.00%5C%22%2C%5C%22amount%5C%22%3A%5C%22216.00%5C%22%7D%2C%7B%5C%22status%5C% 22%3A%5C%22new-record%5C%22%2C%5C%22saldtlid%5C%22%3A%5C%22-10%5C%22%2C%5C%22itemnm%5C%22%3A%5C% 22new+item%5C%22%2C%5C%22qty%5C%22%3A%5C%2210%5C%22%2C%5C%22rate%5C%22%3A%5C%2210.00%5C%22%2C% 5C%22amount%5C%22%3A%5C%22100.00%5C%22%7D%5D%22%7D&csrfmiddlewaretoken=q1ortMN1jyzBIJVn6qOREBLiNr3Lc7njhvI24lT93gLb5VNzWutot0qDeNvlyY5d


实际object包含发票信息和一个csrfmiddlewaretoken 发票数据有 1 header 字典 object 和多个细节字典数组 如何将上面的转换为 python objects 以便我可以处理它

标准 urllib 库可以取消引用它。参见 relevant docs

from urllib import parse
import json
from pprint import pprint


data_in = "%7B%22header%22%3A%22%7B%5C%22salno%5C%22%3A%5C%220%5C%22%2C%5C%22saldt%5C%22%3A%5C%22Wed+Jul+28+2021+00%3A33%3A40+GMT%2B0530+(India+Standard+Time)%5C%22%2C%5C%22soldto%5C%22%3A%5C%22%5C%22%2C%5C%22remark%5C%22%3A%5C%22%5C%22%2C%5C%22totqty%5C%22%3A310%2C%5C%22subtot%5C%22%3A20251%7D%22%2C%22details%22%3A%22%5B%7B%5C%22status%5C%22%3A%5C%22old-modified%5C%22%2C%5C%22saldtlid%5C%22%3A%5C%221%5C%22%2C%5C%22itemnm%5C%22%3A%5C%22item1%5C%22%2C%5C%22qty%5C%22%3A%5C%2218%5C%22%2C%5C%22rate%5C%22%3A%5C%2212.00%5C%22%2C%5C%22amount%5C%22%3A%5C%22216.00%5C%22%7D%2C%7B%5C%22status%5C%22%3A%5C%22new-record%5C%22%2C%5C%22saldtlid%5C%22%3A%5C%22-10%5C%22%2C%5C%22itemnm%5C%22%3A%5C%22new+item%5C%22%2C%5C%22qty%5C%22%3A%5C%2210%5C%22%2C%5C%22rate%5C%22%3A%5C%2210.00%5C%22%2C%5C%22amount%5C%22%3A%5C%22100.00%5C%22%7D%5D%22%7D"
data_out = parse.unquote(data_in)

print(type(data_out))
print(data_out)
print('----------')

data_json = json.loads(data_out)
print(type(data_json))
pprint(data_json, indent=4)

输出

<class 'str'>
{"header":"{\"salno\":\"0\",\"saldt\":\"Wed+Jul+28+2021+00:33:40+GMT+0530+(India+Standard+Time)\",\"soldto\":\"\",\"remark\":\"\",\"totqty\":310,\"subtot\":20251}","details":"[{\"status\":\"old-modified\",\"saldtlid\":\"1\",\"itemnm\":\"item1\",\"qty\":\"18\",\"rate\":\"12.00\",\"amount\":\"216.00\"},{\"status\":\"new-record\",\"saldtlid\":\"-10\",\"itemnm\":\"new+item\",\"qty\":\"10\",\"rate\":\"10.00\",\"amount\":\"100.00\"}]"}
----------
<class 'dict'>
{   'details': '[{"status":"old-modified","saldtlid":"1","itemnm":"item1","qty":"18","rate":"12.00","amount":"216.00"},{"status":"new-record","saldtlid":"-10","itemnm":"new+item","qty":"10","rate":"10.00","amount":"100.00"}]',
    'header': '{"salno":"0","saldt":"Wed+Jul+28+2021+00:33:40+GMT+0530+(India+Standard+Time)","soldto":"","remark":"","totqty":310,"subtot":20251}'}