Python 始终无法正确解析 JSON. [英] Python not parsing JSON correctly all the time.

查看:24
本文介绍了Python 始终无法正确解析 JSON.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 JSON 对象中获取此信息:

呼叫在此处进行:

response = make_request(GET_QUALIFIED_OFFERS_URL, request)def make_request(url, json_data):主机 = 网址req = urllib2.Request(host, json_data, {'content-type': 'application/json'})response_stream = urllib2.urlopen(req)返回 response_stream.read()response = {"Violations":[],"Messages":[],"Log":[],"Session":{"SessionId":813982132},"W3iDeviceId":294294043,"IsAfppOfferwallEnabled":true},skipkeys=True,ensure_ascii=False,sort_keys=True,indent=4}打印 json.dumps((响应), 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: 全局名称 'true' 未定义

看起来有些 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 输出一样创建.也许每个键都有一列,然后每个值都有一行.还有其他人这样做吗?

解决方案

在 Python 中,关键字是 True,而不是 true.区分大小写很重要.通过 dumpsloads 的示例用法:

<预><代码>>>>从 json 导入转储,加载>>>>>>d1 = {'key1':'val1','key2':真,'key3':假}>>>s1 = 转储(d1)>>>d2 = 负载(s1)>>>d1{'key3':假,'key2':真,'key1':'val1'}>>>s1'{"key3": false, "key2": true, "key1": "val1"}'>>>d2{u'key3': False, u'key2': True, u'key1': u'val1'}

Getting this back from a JSON object:

The call is made here:

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)

Getting an error:

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

It looks like some of the JSON isn't correct. I put quotes around the value "true" and it works. So is there any way to put quotes around all the values?

This Works:

response = {"Violations":[],"Messages":[],"Log":[],"Session":{"SessionId":813982132},"W3iDeviceId":294294043,"IsAfppOfferwallEnabled":"true"}, skipkeys=True, ensure_ascii=False, sort_keys=True, indent=4}

The problem is I have JSON like this all over the place with values like false and true with no quotes in huge key--value data sets.

What I am trying to do is take the JSon and make it pretty to be able to compare against it. I am trying to write an automation frame work to test what comes back in the Json. Ideally I would love to create like a csv ouput. Maybe have a column for each key and then have a row for each value. Anyone else doing something like this?

解决方案

In Python the keyword is True, not true. Case sensitivity counts. By way of example usage for both dumps and loads:

>>> from json import dumps, loads
>>>
>>> d1 = {'key1': 'val1', 'key2': True, 'key3': False}
>>> s1 = dumps(d1)
>>> d2 = loads(s1)
>>> d1
{'key3': False, 'key2': True, 'key1': 'val1'}
>>> s1
'{"key3": false, "key2": true, "key1": "val1"}'
>>> d2
{u'key3': False, u'key2': True, u'key1': u'val1'}

这篇关于Python 始终无法正确解析 JSON.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆