Python中不能正确解析JSON所有的时间。 [英] Python not parsing JSON correctly all the time.

查看:188
本文介绍了Python中不能正确解析JSON所有的时间。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从获取一个JSON对象,这回:

Getting this back from a JSON object:

该呼叫此处进行:

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是不正确的。我把周围的值引号真和它的作品。那么,有没有办法把身边所有的值报价?

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?

本作品:

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

问题是我有JSON这样的遍布像虚实与硕大的按键没有引号值的地方 - 值数据集

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.

我所试图做的是采取JSON,使其pretty是能够比较反对。我想写一个自动化框架的工作测试什么回来了JSON。理想的情况是我喜欢创建一个像CSV输出中。也许对每个键的列,然后为每个值的行。任何人都做这样的事情?

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?

推荐答案

在Python中的关键字是,而不是真正。区分大小写计数。通过这两个例子的使用方式转储加载

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天全站免登陆