0
這是我的JSON:ValueError異常在/後/不JSON對象可以被解碼
{
"documents": [
{
"score": 0.5,
"id": "1"
}
],
"errors": []
}
我想知道我怎麼能取「得分」而不將其轉換成字典因爲當我嘗試使用? json.loads它給我以下錯誤:
ValueError at /post/
No JSON object could be decoded
這是我正在使用的代碼。
def GetSentiment(documents):
"Gets the sentiments for a set of documents and returns the
information."
headers = {'Ocp-Apim-Subscription-Key': accessKey}
conn = httplib.HTTPSConnection(uri)
body = json.dumps(documents)
conn.request("POST", path, body, headers)
response = conn.getresponse()
return response.read()
documents = {'documents': [
{'id': '1', 'language': 'en', 'text': caption},
]}
result = GetSentiment(documents)
resp_dict = json.loads(result)
print resp_dict
score = resp_dict["documents"][0]["score"]
return score
(改進了代碼格式,使其更容易閱讀) – Anupam
嗨,告訴我們你想做什麼,因爲也許你可以使用另一種方法 –
@mohammedqudah我只想獲取分數值,因爲我想使用它進行情緒分析。要獲取分數值,我只能使用json.loads和那個bug。 –