2015-09-22 162 views
1

我發佈JSON到Python金字塔服務器,但我無法解析它在服務器端。Python金字塔解析JSON

POST請求是這樣的:

$.ajax({ 
url: "http://localhost:6543/linefollower/7/send_result", 
type: "POST", 
data: '{"results": [{"robot_name": "Satikas", "result": null, "team_nr": 30, "team_name": "IT Vennad", "id": 57}]}', 
contentType: "application/json; charset=utf-8", 
dataType: "json" 
} 

但在服務器端我收到這個時候我做印刷(request.body)

b'%5B%7B%22robot_name%22%3A+%22Satikas%22%2C+%22result%22%3A+null%2C+%22team_nr%22%3A+30%2C+%22team_name%22%3A+%22IT+Vennad%22%2C+%22id%22%3A+57%7D%5D=' 

我應該做的是能夠將發佈的內容解析爲JSON?金字塔的request.json_body何時應該包含解析的json?

回答

2

發送前儘量明確地序列化數據爲JSON:

$.ajax({ 
    url: "http://localhost:6543/linefollower/7/send_result", 
    type: "POST", 
    data: JSON.stringify({"results": [... "team_name": "IT Vennad", "id": 57}]}), 
    contentType: "application/json; charset=utf-8", 
    dataType: "json" 
} 
1

我沒有完美的答案,但我可以告訴你,這是與編碼有關的東西。 str.encode()或u'str也許。你可以開始朝這個方向看。

1

您的數據對象是一個字符串。它應該只是一個對象。刪除評論

data: {"results": [{"robot_name": "Satikas", "result": null, "team_nr": 30, "team_name": "IT Vennad", "id": 57}]},