使用Jsonify时的KeyError

时间:2013-05-10 12:56:57

标签: json google-app-engine python-2.7 flask

我正在使用GAE SDK 1.8

我正试图对以下内容进行jsonify:

{
    'prev': None, 
    'records': 1, 
    'next': None, 
    'start': 'http://127.0.0.1:9080/v1/requests/ag1kZXZ-Y3VybGF0cm9uchQLEgdSZXF1ZXN0GICAgICAkIAIDA/transactions?page=1&per_page=50', 
    'items': [
              {
               'customer': {'href': 'ag1kZXZ-Y3VybGF0cm9uchULEghDdXN0b21lchiAgICAgMDvCAw'}, 
               'response_headers': {
                   'x-powered-by': 'PHP/5.3.8', 
                   'transfer-encoding': 'chunked', 
                   'vary': 'Accept-Encoding', 
                   'server': 'Apache/2.2.20 (Unix)', 
                   'date': 'Thu, 09 May 2013 12:14:44 GMT', 
                   'content-type': 'text/html; charset=UTF-8'
               }, 
               'success': True, 
               'url': u'https://recurly-util.pagodabox.com/recurly-notification',
               'response_status': 200, 
               'created_at': '2013-05-09T12:14:44.446901', 
               'request': {'href': 'ag1kZXZ-Y3VybGF0cm9uchQLEgdSZXF1ZXN0GICAgICAkIAIDA'},
               'response_payload': u'html response removed for brevity',
               'request_payload': u'{"created_at": "2013-05-09 12:14:39.393591", "event": {"data": {"test": "test"}, "name": "Model Save"}}', 
               'method': u'post', 
               'href': 'http://127.0.0.1:9080/v1/transactions/ag1kZXZ-Y3VybGF0cm9uch8LEhJSZXF1ZXN0VHJhbnNhY3Rpb24YgICAgICQoAkM', 
               'trigger': {'href': 'ag1kZXZ-Y3VybGF0cm9uchQLEgdUcmlnZ2VyGICAgICA4L8IDA'}, 
               'request_headers': None, 
               'event': {'href': 'ag1kZXZ-Y3VybGF0cm9uchILEgVFdmVudBiAgICAgMCfCAw'}, 'handler': None}
            ]
}

我正在慢慢追溯

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 224, in Handle
    for chunk in result:
  File "/Users/mbeale/python/curlatron/gae_mini_profiler/profiler.py", line 542, in __call__
    for value in result:
  File "/Users/mbeale/python/curlatron/gae_mini_profiler/profiler.py", line 418, in profile_start_response
    yield result_fxn_wrapper(result.next)
  File "/Users/mbeale/python/curlatron/gae_mini_profiler/instrumented_profiler.py", line 70, in run
    return self.c_profile.runcall(lambda *args, **kwargs: fxn(), None, None)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.py", line 149, in runcall
  File "/Users/mbeale/python/curlatron/gae_mini_profiler/instrumented_profiler.py", line 70, in <lambda>
    return self.c_profile.runcall(lambda *args, **kwargs: fxn(), None, None)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/appstats/recording.py", line 1289, in appstats_wsgi_wrapper
    for value in result:
  File "/Users/mbeale/python/curlatron/werkzeug/debug/__init__.py", line 98, in debug_application
    ignore_system_exceptions=True)
  File "/Users/mbeale/python/curlatron/werkzeug/debug/tbtools.py", line 155, in get_current_traceback
    tb = Traceback(exc_type, exc_value, tb)
  File "/Users/mbeale/python/curlatron/werkzeug/debug/tbtools.py", line 206, in __init__
    self.frames.append(Frame(exc_type, exc_value, tb))
  File "/Users/mbeale/python/curlatron/werkzeug/debug/tbtools.py", line 347, in __init__
    fn = inspect.getsourcefile(tb) or inspect.getfile(tb)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 456, in getsourcefile
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 506, in getmodule
KeyError: '__main__'

我已将问题缩小到response_headers字段。当我删除,一切正常。我也觉得很奇怪的是,如果我用完全相同的字段替换那个字段,它就能正常工作而没有错误。

collection['items'][0]['response_headers'] = {'x-powered-by': 'PHP/5.3.8', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'Apache/2.2.20 (Unix)', 'date': 'Thu, 09 May 2013 12:15:01 GMT', 'content-type': 'text/html; charset=UTF-8'

我正在使用ndb.PickleProperty将值存储在数据存储区中。我正在检索以下信息:

    resp = urlfetch.fetch(rt.url, payload=rt.request_payload, method=rt.method, headers=new_headers, allow_truncated=False, follow_redirects=False, deadline=30)
    if resp.status_code < 299 and resp.status_code > 199:
        rt.success = True
        req.state = "completed"
        req.put()
        logging.info('Success:%s' % resp.status_code)
    rt.response_status = resp.status_code
    rt.response_headers = resp.headers

为什么我收到此错误?在保存到数据存储区之前,我应该对返回的响应头进行一些编码吗?如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

我的问题是默认情况下ndb.PickleProperty没有给我有效的JSON。我不得不做dict(model.response_header)来获得有效的JSON。