如何在python中将数据解析为正确的json并删除斜杠

时间:2015-10-05 22:21:18

标签: python arrays json

我将此作为我的回复

(function() {
  var button, output;

  function scheduleNotifyTheUser() {
    output.innerHTML = "Stand by…";
    setTimeout(notifyTheUser, 1000);
  }
  
  function notifyTheUser() {
    output.innerHTML = "Ding! You have a message!";
    new Notification("Something Happened", {
      body: "You should totally check this out!"
    });
  }
  
  document.addEventListener("DOMContentLoaded", function() {
    button = document.getElementById("gobtn");
    output = document.getElementById("output");
    output.innerHTML = "Requesting permission…";
    Notification.requestPermission(function(permission) {
      if (permission !== "granted") {
        output.innerHTML = "Notifications disabled.";
        return;
      }
      button.addEventListener("click", scheduleNotifyTheUser);
      output.innerHTML = "Ready.";
    });
  });
})();

我想将它转换为适当的json而不需要任何双斜线。

是否有任何buildin函数或我需要进行字符串替换

2 个答案:

答案 0 :(得分:0)

如果您可以控制它的发送方式,我建议您在作为json发送的任何相关字段/键上执行to_string。在清理json_dump的输入之前,我有一些奇怪的json响应。

答案 1 :(得分:-1)

删除前导b并运行替换,如下所示。

s = '{"userdetails":[["{\\"user_id\\":[\\"54562af66ffd\\"],\\"user_name\\":[\\"bewwrking\\"],\\"room\\":[\\"31\\"]}'

s = s.replace('\','')

print(s)

{"userdetails":[["{"user_id":["54562af66ffd"],"user_name":["bewwrking"],"room":["31"]}