python simpleJSONDecoder和复杂的JSON问题 [英] python simpleJSONDecoder and complex JSON issue

查看:104
本文介绍了python simpleJSONDecoder和复杂的JSON问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在运行的单元测试用例中,我在下面的json文本中的第4个json对象上遇到KeyError异常,因为负责解码的代码段正在寻找一个不存在的对象,但是应该.

In a unit test case that I am running, I get a KeyError exception on the 4th json object in the json text below because the piece of code responsible for decoding is looking for an object that isn't there, but should be.

我遍历了子对象,发现是导致问题的"cpuid"对象.当我删除它并运行测试时,它工作正常.

I went through the sub-objects and found that it was the "cpuid" object that causes the problem. When I remove it and run the test, it works fine.

def _make_report_entry(记录):

def _make_report_entry(record):

    response = self.app.post(
        '/machinestats',
        params=dict(record=self.json_encode([
                    {"type": "crash", "instance_id": "xxx",
                     "version": "0.2.0", "build_id": "unknown",
                     "crash_text": "Gah!"},
                    {"type": "machine_info", "machine_info": "I'm awesome.",
                     "version": "0.2.0", "build_id": "unknown",
                     "instance_id": "yyy"},
                    {"machine_info": "Soup", "crash_text": "boom!",
                     "version": "0.2.0", "build_id": "unknown",
                     "instance_id": "zzz", "type": "crash"},
                    {"build_id" : "unknown", "cpu_brand" : "intel",
                     "cpu_count" : 4,
                     "cpuid": {
                               "00000000":
                                    {"eax" :123,"ebx" :456,
                                     "ecx" :789,"edx" :321},
                               "00000001":
                                    {"eax" :123,"ebx" :456,
                                     "ecx" :789,"edx" :321}},
                     "driver_installed" : True,
                     "instance_id" : "yyy",
                     "version" : "0.2.0",
                     "machine_info" : "I'm awesome.",
                     "os_version" : "linux",
                     "physical_memory_mib" : 1024,
                     "product_loaded" : True,
                     "type" : "machine_info",
                     "virtualization_advertised" : True}
                    ])))

在要测试的代码段中,我使用了django.utils中的simplejson.JSONDecoder来解码JSON.当我记录传递给我的解码函数的上述JSON的解码输出时,得到以下信息:

In the piece of code being tested, I use simplejson.JSONDecoder from django.utils to decode the JSON. When I log the decoded output for the above JSON that gets passed to my decoding function, I get this:

root:INFO:{u'instance_id':u'xxx',u'type':u'crash',u'crash_text':u'Gah!',u'version':u'0.2.0' ,u'build_id':u'unknown'}

root: INFO: {u'instance_id': u'xxx', u'type': u'crash', u'crash_text': u'Gah!', u'version': u'0.2.0', u'build_id': u'unknown'}

root:INFO:{u'build_id':u'unknown',u'instance_id':u'yyy',u'version':u'0.2.0',u'machine_info':u"我是棒极了.,u'type':u'machine_info'}

root: INFO: {u'build_id': u'unknown', u'instance_id': u'yyy', u'version': u'0.2.0', u'machine_info': u"I'm awesome.", u'type': u'machine_info'}

root:INFO:{u'build_id':u'unknown',u'machine_info':u'Soup',u'version':u'0.2.0',u'instance_id':u'zzz', u'crash_text':u'boom!',u'type':u'crash'}

root: INFO: {u'build_id': u'unknown', u'machine_info': u'Soup', u'version': u'0.2.0', u'instance_id': u'zzz', u'crash_text': u'boom!', u'type': u'crash'}

root:INFO:{u'eax':123,u'edx':321,u'ebx':456,u'ecx':789}

root: INFO: {u'eax': 123, u'edx': 321, u'ebx': 456, u'ecx': 789}

在最后一个JSON对象上,仅JSON cpuid对象内的对象被传递给我的解码函数.因为我的解码功能期望其他对象(例如'type','instance_id'等),所以出现了KeyError异常.

On the last JSON object, only the object within the JSON cpuid object is being passed to my decoding function. Because my decoding function is expecting the the other objects (e.g., 'type', 'instance_id', etc.), I get a KeyError exception.

[很抱歉,以前的篇幅太长了,我希望这能使它的范围缩小一些]

[Sorry for the earlier unnecessarily long post, I hope that this will narrow it down a bit more]

推荐答案

复制并粘贴要传递给self.json_encode的内容,并将其用作json.dumps的参数(在python 2.6中为import json之后) ,效果很好.因此,似乎错误可能出在您未向我们展示的json_encode方法中:除了调用json.dumps ...以外,它还有什么作用? (当然,如果您使用的是python< 2.6,则为simplejson.dumps.)

Copying and pasting what you're passing to self.json_encode, and using it as an argument of json.dumps (after an import json in Python 2.6), works just fine. So it seems the bug may be in the json_encode method you're not showing us: what else does it do, besides just calling json.dumps...? (or simplejson.dumps if you're using a Python < 2.6, of course).

编辑:使用json_encode = json.JSONEncoder().encode(就像刚刚发布的OP一样,不同的是使用了较老的simplejson,正如我前面提到的那样)也可以正常工作. Q的较大修改的一部分还显示了不完整的堆栈跟踪,这表明该错误来自 decoding 部分,可能是由于某些模型的误用(无法分辨,因为我们看不到模型)-正如OP所说的那样,他现在发布了更多信息,但仍然不足以调试问题.

Edit: using json_encode = json.JSONEncoder().encode (as the OP just posted, except that's using the older simplejson as I had mentioned as a possibility) also works fine. The incomplete stack-trace also posted as part of the Q's large edit suggests that the error comes in the decoding part, perhaps through misuse of some model (can't tell, as we don't see the models) -- as the OP mentioned he's now posted a lot more info, and yet it's still not enough to debug the problem.

这强烈表明OP值得一次尝试稍微简化一下问题,直到最后一次逐步简化使错误消失为止-通常强烈暗示错误可能是什么,即使并非如此,发布最小的方法来重现该错误,以及如果进一步删除少量的epsilon代码,该错误将消失的信息,可能会像我们所有人一样帮助第三方观察员"协助调试. SO并不是真正的用于集体调试的平台(更好地用于问题和答案,它是为设计的),但是我认为它不会违反SO的规则来尝试将其用于不同的目的

This strongly suggests that it would be worthwhile for the OP to try and simplify the problem a little at a time until the last incremental simplification makes the bug disappear -- that usually strongly hints at what the bug may be, but even if it doesn't, posting the tiniest way to reproduce the bug plus the info that the bug will disappear if a miniscule epsilon of code is further removes, may help "third party observers" like us all assist in the debugging. SO is not really a platform designed for collective debugging (works better for questions and answers, what it was designed for) but I don't think it breaks SO's rules to try and use it for this different purpose.

这篇关于python simpleJSONDecoder和复杂的JSON问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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