首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jsonify时的KeyError

使用Jsonify时的KeyError
EN

Stack Overflow用户
提问于 2013-05-10 20:56:57
回答 1查看 750关注 0票数 1

我使用的是GAE SDK 1.8

我试着在以下几个方面进行说明:

代码语言:javascript
复制
{
    '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}
            ]
}

我得到了休眠回溯

代码语言:javascript
复制
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字段。当我移除它的时候,一切都很正常。我还发现奇怪的是,如果我用完全相同的字段替换该字段,它可以正常工作而不会出现错误。

代码语言:javascript
复制
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将值存储在数据存储中。我从以下位置检索信息:

代码语言:javascript
复制
    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

为什么我会得到这个错误?在保存到数据存储之前,是否应该对返回的响应头进行编码?如果您需要更多信息,请让我知道。

EN

回答 1

Stack Overflow用户

发布于 2013-05-11 06:32:39

我的问题是ndb.PickleProperty在默认情况下没有给我有效的JSON。我必须使用dict(model.response_header)才能获得有效的JSON。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16482803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档