在python服务器端解析json

时间:2016-05-19 11:40:11

标签: 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循环。请告诉我如何解决这个问题。我的结果变量看起来像目前在片段中所示。帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

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

result = json.loads(body)