javascript - 在python服务器端解析json

标签 javascript python json

我的客户端代码:

var http = new XMLHttpRequest();
    var url = "http://localhost:8000/recData";
    var params = JSON.stringify({json: screen_keywords});
    console.log(params);
    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    http.onreadystatechange = function() {
    //Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }   
    }
    http.send(params);

我的服务器:

def do_POST(self):
        if self.path == "/recData":
          content_length = int(self.headers.getheader('content-length'))        
          body = self.rfile.read(content_length)
          result = json.dumps(body)
          pprint(result)

结果是:

'"{\\"json\\":[{\\"id\\":\\"1453-HCI-user-human-computer-interaction\\",\\"x_cordinate\\":395.9217224121094,\\"y_cordinate\\":729.04296875,\\"keyword\\":\\"user-human-computer-interaction\\",\\"radius\\":\\"70.359375\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"4044-HCI-user-design\\",\\"x_cordinate\\":544.33837890625,\\"y_cordinate\\":773.467529296875,\\"keyword\\":\\"user-design\\",\\"radius\\":\\"50.359375\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"668-HCI-user-experience\\",\\"x_cordinate\\":328.0223083496094,\\"y_cordinate\\":460.09271240234375,\\"keyword\\":\\"user-experience\\",\\"radius\\":\\"73.703125\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"2714-HCI-user-enjoyment\\",\\"x_cordinate\\":562.9501342773438,\\"y_cordinate\\":268.96636962890625,\\"keyword\\":\\"user-enjoyment\\",\\"radius\\":\\"70.359375\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"2659-ip-ip-TCP\\",\\"x_cordinate\\":648.444091796875,\\"y_cordinate\\":813.05712890625,\\"keyword\\":\\"ip-TCP\\",\\"radius\\":\\"41\\",\\"obj_rel\\":true,\\"sub_rel\\":false},{\\"id\\":\\"1022-service-ecosystem-trust\\",\\"x_cordinate\\":743.697509765625,\\"y_cordinate\\":848.6085205078125,\\"keyword\\":\\"trust\\",\\"radius\\":\\"38.3359375\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"1916-ip-ip-management\\",\\"x_cordinate\\":750.01123046875,\\"y_cordinate\\":681.5203247070312,\\"keyword\\":\\"ip-management\\",\\"radius\\":\\"85.0390625\\",\\"obj_rel\\":true,\\"sub_rel\\":true},{\\"id\\":\\"1769-service-ecosystem-service-design\\",\\"x_cordinate\\":653.7368774414062,\\"y_cordinate\\":903.925537109375,\\"keyword\\":\\"service-design\\",\\"radius\\":\\"53.0078125\\",\\"obj_rel\\":false,\\"sub_rel\\":true},{\\"id\\":\\"4516-HCI-user-fun\\",\\"x_cordinate\\":562.857177734375,\\"y_cordinate\\":884.592529296875,\\"keyword\\":\\"user-fun\\",\\"radius\\":\\"41\\",\\"obj_rel\\":false,\\"sub_rel\\":true},{\\"id\\":\\"2291-HCI-user-usability\\",\\"x_cordinate\\":436.70556640625,\\"y_cordinate\\":878.2095947265625,\\"keyword\\":\\"user-usability\\",\\"radius\\":\\"58.3515625\\",\\"obj_rel\\":false,\\"sub_rel\\":true}]}"'

我正在将 json 发送到我的服务器端,但我无法在服务器端对其进行解码。因为我想将它用作列表,所以我可以通过访问每个对象在 python 端使用 for 循环。请告诉我如何解决这个问题。我的结果变量目前看起来像这样,如代码片段中所示。我们将非常感谢您的帮助。

最佳答案

您正在尝试重新编码 JSON 字符串。您需要使用json.loads()相反:

result = json.loads(body)

关于javascript - 在python服务器端解析json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37322106/

相关文章:

python 以分钟为单位获取时间

python - 如何从该应用程序中调用 Python Bottle API

java - 动态更改 bean 中的键名到 JSON 转换

c# - 是微软的 Json (System.Runtime.Serialization.Json) 错误吗?

python - 如何使用 Python 3 json.dumps 保持固定的 JSON 键顺序?

javascript - 自定义结账字段: Hiding/showing fields with a selector

javascript - 如何在两列页面中停止 div 换行

javascript - Reactjs 中的 maxLength

javascript - 如何以编程方式将选项添加到 selectize.js 中的选项组

python - 原始输入处的 Nose 测试卡住