2013-05-10 120 views
1

我使用GAE SDK 1.8KeyError异常使用Jsonify

我想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

您提供的实际上是显示错误的堆栈跟踪被抛出在调试代码时,被巡视寻找一个源文件'__main__',它没有很多工作要做,失败的东西转换成JSON。 – 2013-05-10 15:00:53

回答

1

我的问题是,默认情况下ndb.PickleProperty没有给我有效的JSON。我必须做dict(model.response_header)才能拥有有效的JSON。