简体   繁体   English

python function .json():无法解码JSON对象

[英]python function .json(): No JSON object could be decoded

the function looks like this: 该函数如下所示:

import requests
import json
def parse(s):
    r = requests.post('http://166.111.139.15:9000/?properties%3d%7b%22annotators%22%3a%22tokenize%2cssplit%2cpos%2clemma%2cparse%22%2c%22outputFormat%22%3a%22json%22%7d%0a', data=s)
    return r.json()

print parse("I am a student")

And when I use it in Django, the web page shows: "No JSON object could be decoded".Why? 当我在Django中使用它时,网页显示:“无法解码JSON对象”。为什么?

No JSON object could be decoded is an exception message raised at r.json() . r.json()引发异常消息时, No JSON object could be decoded If your response is not a valid json object you can still retrieve it with r.text . 如果您的响应不是有效的json对象,您仍然可以使用r.text检索它。 Even if you are sure your response is always a valid json object, you should still check whether server returned a success code. 即使您确定响应始终是有效的json对象,也应检查服务器是否返回成功代码。 If there's an internal server error (code 500), you won't get a valid json response! 如果发生内部服务器错误(代码500),您将无法获得有效的json响应!

import requests

def parse(s)
    r = requests.post('http://someserver.com', data=s)
    if r.status_code !== 200:
        return "There was a problem: {} !".format(r.text)
    return r.json()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM