weixin_33676492 2015-08-06 18:59 采纳率: 0%
浏览 46

如何解析无效的JSON

I have a data-set and I have requested that it is sent to me in JSON. This data-set however contains a backslash which causes issues when I am trying to parse it. Other then fixing the JSON file, is there anything I can do in JS (ie - escape the slash)?

data.json:

{
    "Name": "Wonderful Buffet",
    "Address": "1234 Main Street \"
}

script.js:

$.ajax('data.json').done( function(data) {
    console.log(data); //Outputs nothing
});
  • 写回答

1条回答 默认 最新

  • weixin_33722405 2015-08-06 19:22
    关注

    You can escape a \ by replacing it with \\. F.e:

    var myVar = "one\\slash";
    alert (myVar);

    But this should be done on the provider side, not on the receiving end. People shouldn't be sending you unparseable JSON, that's just bad manners.

    </div>
    
    评论

报告相同问题?