Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python JSON解析错误";非类型对象不可调用";?_Python_Json - Fatal编程技术网

Python JSON解析错误";非类型对象不可调用";?

Python JSON解析错误";非类型对象不可调用";?,python,json,Python,Json,代码: body = '%s' % message.get_body() logging.error(body) parsed_body = json.loads(body) 正文包含: body = '%s' % message.get_body() logging.error(body) parsed

代码:

                body = '%s' % message.get_body()
                logging.error(body)
                parsed_body = json.loads(body)
正文包含:

                body = '%s' % message.get_body()
                logging.error(body)
                parsed_body = json.loads(body)
[{u'content':u'Test\r\n',u'body\u section':1,u'charset':u'utf-8',u'type':u'text/plain'},{u'content':u'Test\r\n',u'body\u section':2,u'charset':u'utf-8',u'type':u'text/html'}]

错误:

line 67, in get
    parsed_body = json.loads(body)
  File "C:\Python27\lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "C:\Python27\lib\json\decoder.py", line 38, in errmsg
    lineno, colno = linecol(doc, pos)
TypeError: 'NoneType' object is not callable

有人知道怎么回事吗?

正文
是一个字符串(您通过“%s”…)获得它),因此它不应包含Python字符串编码,例如
u'content'

这意味着要么get_body()返回一个复杂的对象,
'%s'%..
正在将其转换为一个python输出字符串,而该字符串不是JSON,要么get_body返回该字符串时,某个东西已经在“修复”该字符串。错误就在别处

例如:

import json

# This is a correct JSON blob

body = '[{"content": "Test\\r\\n", "body_section": 1, "charset": "utf-8", "type": "text/plain"}, {"content": "Test\\r\\n", "body_section": 2
, "charset": "utf-8", "type": "text/html"}]'

# And this is a correct object
data = json.loads(body)

# If I were to print this object into a string, I would get 
# [{u'content': u'Test\r\n', u'type': u'text/plain', u'charset'...
# and a subsequent json.load would fail.

# Remove the comment to check whether the error is the same (it is not on
# my system, but I'm betting it depends on JSON implementation and/or python version)

# body = '%s' % data

print body

print json.loads(body)

我想body可能是None?get_body是否已返回字符串?是的。我只是在尝试一些随机的东西来修复它。你是对的。我现在觉得自己很愚蠢。它已经被解析成一个包含dict的列表。当我记录json和作为字符串打印的对象之间的内容时,如何区分两者之间的差异?u'stuff'是一个很好的告密者吗?好吧,我已经被这个特殊的虫子咬了很多次,我不再流血了。是的,我通常检查u'。是的,我也觉得自己很愚蠢。每次:-(@Joren I经常
打印(键入(obj))
以确保。