从 JSON 对象中获取它:
调用在这里:
response = make_request(GET_QUALIFIED_OFFERS_URL, request)
def make_request(url, json_data):
host = url
req = urllib2.Request(host, json_data, {'content-type': 'application/json'})
response_stream = urllib2.urlopen(req)
return response_stream.read()
response = {"Violations":[],"Messages":[],"Log":[],"Session":{"SessionId":813982132},"W3iDeviceId":294294043,"IsAfppOfferwallEnabled":true}, skipkeys=True, ensure_ascii=False, sort_keys=True, indent=4}
print json.dumps((response), sort_keys=True, indent=4)
收到错误:
print json.dumps({"Violations":[],"Messages":[],"Log":[],"Session":{"SessionId":813982132},"W3iDeviceId":294294043,"IsAfppOfferwallEnabled":true}, skipkeys=True, ensure_ascii=False, sort_keys=True, indent=4)
NameError: global name 'true' is not defined
看起来有些 JSON 不正确。我在值“true”周围加上引号,它起作用了。那么有没有办法在所有值周围加上引号?
这有效:
response = {"Violations":[],"Messages":[],"Log":[],"Session":{"SessionId":813982132},"W3iDeviceId":294294043,"IsAfppOfferwallEnabled":"true"}, skipkeys=True, ensure_ascii=False, sort_keys=True, indent=4}
问题是我到处都有这样的 JSON,其值如 false 和 true,在巨大的键值数据集中没有引号。
我正在尝试做的是采用 JSon 并使其变得漂亮以便能够与之进行比较。我正在尝试编写一个自动化框架来测试 Json 中返回的内容。理想情况下,我很想创建一个 csv 输出。也许每个键都有一列,然后每个值都有一行。还有其他人在做这样的事情吗?