更新有效负载以将True更改为True

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

我有这个有效载荷:

payload = {"data":{"horse_name": "beauty", "horse_id":"52", "is_manual":true }}

此返回:

> NameError: name 'true' is not defined

...这是正确的。

如何更改上述有效载荷以使其为True,使其看起来像:

payload = {"data":{"horse_name": "beauty", "horse_id":"52", "is_manual":True }}
python
1个回答
0
投票

可能有更好的解决方案;

import json
from collections import namedtuple

data = {"data":{"horse_name": "beauty", "horse_id":"52", "is_manual":true }}

# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
x.data.is_manual = True
print x

未测试。

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