简体   繁体   English

JSON 解析偶尔会在解析字符串时出错

[英]JSON parse gives error occasionally on parsing string

I have same input fields where the user inputs some data.我有相同的输入字段,用户可以在其中输入一些数据。 Next my code converts it to JSON and send it to the server.接下来,我的代码将其转换为 JSON 并将其发送到服务器。

$("#submit").click(function(){
    var inp = $("#inpTxt").val();

    if(inp == null || inp == ""){
        return;
    }

    jsonResult = JSON.parse('{"data": "' + inp + '"}');

    $.ajax({
        data : jsonResult,
        ...
    });
});

The issue here is that sometimes the above code works and sometimes it just don't.这里的问题是有时上面的代码有效,有时却不起作用。 Most of the time I tested the code it worked like a charm but on productions I keep on getting the error reported several times.大多数时候,我测试了代码,它就像一个魅力,但在生产中,我不断收到多次报告的错误。

I have not been able to figure out the possible cause yet.我还没有弄清楚可能的原因。

Note: Some parts of the code are not shared in above but only part of what the problem seems to be.注意:代码的某些部分在上面没有共享,而只是问题的一部分。

As per the last comment the problem seems to be occurring when someone inputs some escape character in input (@Noob46 identified the case of using "Something" case but in my opinion it will also occur if any other escape character like \\ or any other starting with \\ may also cause the same problem).根据最后一条评论,当有人在输入中输入一些转义字符时,问题似乎发生了(@Noob46 确定了使用“Something”案例的情况,但在我看来,如果任何其他转义字符,如 \\ 或任何其他开头与 \\ 也可能导致同样的问题)。

Thus, one way to handle this is to filter the coming input string before passing it to JSON.parse.因此,处理此问题的一种方法是在将传入的输入字符串传递给 JSON.parse 之前对其进行过滤。

To take things a step forward I have created some js that you can find here which in fact will let you handle the escape characters if they come as part of your input.为了更进一步,我创建了一些 js,您可以在此处找到它们,如果它们作为输入的一部分出现,实际上可以让您处理转义字符。

For further details refer to the attached code and example in the provided link here .有关更多详细信息,请参阅此处提供的链接中的附加代码和示例。

As the problem identified by me so let me also state an answer here.由于我确定的问题,所以让我也在这里陈述一个答案。 All you need is to add an additional \\ before all the escape characters you encounter like:您只需要在遇到的所有转义字符之前添加一个额外的 \\ ,例如:

\\b : backspace \\b : 退格

\\f : form feed \\f : 换页

\\n : line feed \\n : 换行

\\r : carriage return \\r : 回车

\\t : horizontal tab \\t : 水平制表符

\\v : vertical tab " : double quote \\ : back slash \\v : 垂直制表符 " : 双引号 \\ : 反斜杠

You can do so by code like:您可以通过以下代码执行此操作:

inp = inp.replace('"','\\\"');

Or inp.replace('\\','\\\\\\\\');或者inp.replace('\\','\\\\\\\\');

And so on for other characters.其他角色依此类推。

When you use JSON.Parse function you need to handle special characters by your self.当您使用 JSON.Parse 函数时,您需要自己处理特殊字符。 So if someone puts a " in your inpTxt input it will cause an error on parse that string.因此,如果有人在您的 inpTxt 输入中放入 " ,则会导致解析该字符串时出错。

You should do something like the code below to avoid parse error and let javascript correctly handle special characters on your string.您应该执行类似以下代码的操作以避免解析错误并让 javascript 正确处理字符串中的特殊字符。

jsonResult = { data: inp };

I create a JS fiddle to make it easier to understand.我创建了一个 JS 小提琴以使其更容易理解。 https://jsfiddle.net/ub3w0a8x/ https://jsfiddle.net/ub3w0a8x/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM