mod-wsgi在尝试将字符串参数转换为json时出错

时间:2015-08-12 08:29:17

标签: python json mod-wsgi

我收到以下错误:

File "C:\\Python34\\Lib\\json\\decoder.py", line 361, in raw_decode\r
raise ValueError(errmsg("Expecting value", s, err.value)) from None\r
ValueError: Expecting value: line 1 column 1 (char 0)\r, referer

当我使用xmlhttp发送以下参数时:

xmlhttp.send( "{ \"field1\": 'hello', \"field2\" : 'hello2'}" ); 

mod_wsgi代码:

def application(environ, start_response):

    output = ChildClass().getValue()
    print( output)
    request_body_size = int(environ['CONTENT_LENGTH'])
    request_body = environ['wsgi.input'].read(request_body_size)
    status = '200 OK'
    strBody = str(request_body)
    jsnBody = json.loads(strBody )
    stroutput = '@' + strBody 
    for iterating_var in output:

        values = ','.join(str(v) for v in iterating_var)
        #str = ''.join(output[0])
        print('second  ' + values)
        stroutput +=  '&&' + values
        #print(str.encode('UTF-8'))

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(stroutput)))]
    start_response(status, response_headers)
    return [stroutput.encode('UTF-8')]

有问题的行:

strBody = str(request_body)
 jsnBody = json.loads(strBody )

1 个答案:

答案 0 :(得分:0)

当在要加载的字符串中使用混合引号时,Python的json解码器会引发一个神秘的错误。

import json
json.loads('{"thing": "value"}')
json.loads('{\'thing\': \'value\'}') #json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
json.loads('{"thing": \'value\'}') # json.decoder.JSONDecodeError: Expecting value: line 1 column 11 (char 10)is formatted with
json.loads("{\"thing\": 'value'}") # json.decoder.JSONDecodeError: Expecting value: line 1 column 11 (char 10)

对json中的字符串使用"而不是'可以解决您的错误。