如何将文本转换为 Python 中的字典列表
How to convert text to list of dict in Python
我有这样的文本结果:
{"title": "Less Payment", "outstanding": false, "content": [{"name": "Customer Payment: INV/2020/0002", "journal_name": "Cash", "amount": 50000.0, "currency": "Rp", "digits": [69, 2], "position": "before", "date": "2020-04-29", "payment_id": 13, "account_payment_id": 3, "payment_method_name": null, "move_id": 9, "ref": "CSH1/2020/0003 (INV/2020/0002)"}, {"name": "Customer Payment: INV/2020/0003", "journal_name": "Cash", "amount": 15000.0, "currency": "Rp", "digits": [69, 2], "position": "before", "date": "2020-04-29", "payment_id": 18, "account_payment_id": 4, "payment_method_name": null, "move_id": 11, "ref": "CSH1/2020/0004 (INV/2020/0003)"}]}
看起来是字典,实际上那个结果的数据类型是字符串。
我想将其转换为正确的格式以便我可以处理数据。
如何做到这一点..?或者任何教程或资源来了解这..?
有关更多信息,我从 Odoo13 mail.template 得到了结果,然后我想从invoice_payments_widget 来自 account.move 对象的字段。
import json
my_json_string = "{'hi':5}"
res = json.loads(my_json_string)
print(res['hi']) # 5
import json
x='{"title": "Less Payment", "outstanding": false, "content": [{"name": "Customer Payment: INV/2020/0002", "journal_name": "Cash", "amount": 50000.0, "currency": "Rp", "digits": [69, 2], "position": "before", "date": "2020-04-29", "payment_id": 13, "account_payment_id": 3, "payment_method_name": null, "move_id": 9, "ref": "CSH1/2020/0003 (INV/2020/0002)"}, {"name": "Customer Payment: INV/2020/0003", "journal_name": "Cash", "amount": 15000.0, "currency": "Rp", "digits": [69, 2], "position": "before", "date": "2020-04-29", "payment_id": 18, "account_payment_id": 4, "payment_method_name": null, "move_id": 11, "ref": "CSH1/2020/0004 (INV/2020/0003)"}]}'
x=json.loads(x)
我有这样的文本结果:
{"title": "Less Payment", "outstanding": false, "content": [{"name": "Customer Payment: INV/2020/0002", "journal_name": "Cash", "amount": 50000.0, "currency": "Rp", "digits": [69, 2], "position": "before", "date": "2020-04-29", "payment_id": 13, "account_payment_id": 3, "payment_method_name": null, "move_id": 9, "ref": "CSH1/2020/0003 (INV/2020/0002)"}, {"name": "Customer Payment: INV/2020/0003", "journal_name": "Cash", "amount": 15000.0, "currency": "Rp", "digits": [69, 2], "position": "before", "date": "2020-04-29", "payment_id": 18, "account_payment_id": 4, "payment_method_name": null, "move_id": 11, "ref": "CSH1/2020/0004 (INV/2020/0003)"}]}
看起来是字典,实际上那个结果的数据类型是字符串。
我想将其转换为正确的格式以便我可以处理数据。
如何做到这一点..?或者任何教程或资源来了解这..?
有关更多信息,我从 Odoo13 mail.template 得到了结果,然后我想从invoice_payments_widget 来自 account.move 对象的字段。
import json
my_json_string = "{'hi':5}"
res = json.loads(my_json_string)
print(res['hi']) # 5
import json
x='{"title": "Less Payment", "outstanding": false, "content": [{"name": "Customer Payment: INV/2020/0002", "journal_name": "Cash", "amount": 50000.0, "currency": "Rp", "digits": [69, 2], "position": "before", "date": "2020-04-29", "payment_id": 13, "account_payment_id": 3, "payment_method_name": null, "move_id": 9, "ref": "CSH1/2020/0003 (INV/2020/0002)"}, {"name": "Customer Payment: INV/2020/0003", "journal_name": "Cash", "amount": 15000.0, "currency": "Rp", "digits": [69, 2], "position": "before", "date": "2020-04-29", "payment_id": 18, "account_payment_id": 4, "payment_method_name": null, "move_id": 11, "ref": "CSH1/2020/0004 (INV/2020/0003)"}]}'
x=json.loads(x)