简体   繁体   English

JSON.parse无法识别要解析的字符串中的\\“(反斜杠和引号)

[英]JSON.parse can't recognize \" (backslash and quote) in string to parse

I have serialized JSON with objects that have quotes in string like this: 我已经将对象的JSON序列化为字符串,如下所示:

var bla = [
    { 
        "label": "person", 
        "id": "1", 
        "data": "bla means: \"lala\"" 
    }
];

I know how it looks on server side. 我知道它在服务器端的外观。 But when I debug it in browser I see that I receive error on this because it looks like below: 但是,当我在浏览器中对其进行调试时,我看到在此上我收到错误,因为它看起来如下:

var bla = [
    { 
        "label": "person", 
        "id": "1", 
        "data": "bla means: "lala"" 
    }
];

and my error : 和我的错误:

Uncaught SyntaxError: Unexpected token l in JSON at position 25 at JSON.parse () Uncaught SyntaxError:JSON中意外的令牌l在JSON.parse()的位置25

I read that JSON should recognize \\" https://www.json.org/ 我读到JSON应该可以识别\\“ https://www.json.org/

Any ideas where should I correct it? 有什么想法我应该在哪里纠正? On server side when I serialized object or on client side with JavaScript? 在服务器端我序列化对象时还是在客户端使用JavaScript?

EDIT: 编辑:

On server side I use C# library "System.Web.Script.Serialization" The object to serialize: 在服务器端,我使用C#库"System.Web.Script.Serialization"进行序列化的对象:

List<SomeObject> someObject= new List<SomeObject>();
        SomeObject bla1 = new SomeObject();
        bla1.id = "1";
        bla1.label = "person";
        bla1.label = "bla means: \"lala\"";
        someObject.Add(bla1);

var bla = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(someObject);

SUMMARY 摘要

I fix in on serialized string on server side, because I use API where I can't manipulate this objects. 我在服务器端修复了序列化的字符串,因为我使用的API无法处理此对象。 var bla = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(someObject).Replace("\\\\\\"", "\\\\\\\\\\\\\\"");

You can try this with double backslash or triple backslash like \\\\" or \\\\\\" 您可以尝试使用双反斜杠或三反斜杠,例如\\\\" or \\\\\\"

var bla = [
    { 
        "label": "person", 
        "id": "1", 
        "data": "bla means: \\"lala\\"" 
    }
];

It will add backslash and print it with double quotes. 它将添加反斜杠并用双引号打印。

The server should serve proper serialized JSON that can be parsed directly, without the client having to do tricky string manipulations on the data first. 服务器应提供可直接解析的正确的序列化JSON,而客户端不必首先对数据进行棘手的字符串操作。 (For example, if you don't fix it server-side, then it becomes invalid to fetch(...).then(res => res.json()) ) (例如,如果您不在服务器端对其进行修复,则fetch(...).then(res => res.json())变得无效fetch(...).then(res => res.json())

I didn't fully understand your project's structure, but: 我不完全了解您的项目的结构,但是:

backslash \\ is a special character in javascript. 反斜杠 \\是javascript中的特殊字符。

It is special because it has a regex special meaning. 之所以特殊是因为它具有正则表达式的特殊含义。

  1. When writing \\hello , your output would be hello 编写\\hello ,您的输出将是hello
  2. When writing \\'hello , your output would be 'hello . 编写\\'hello ,您的输出将是'hello
  3. When writing \\"hello\\" , your output would be "hello" 编写\\"hello\\" ,输出为"hello"

I think you should first choose how exactly you want your data property to be formatted. 我认为您应该首先选择要如何精确格式化data属性。

Do you want data to be a string? 您要数据为字符串吗? array? 数组? object? 宾语? a string which keeps all of its quotation marks as double or single? 保留所有引号为双引号或单引号的字符串?

Have a look here - https://www.w3schools.com/js/js_strings.asp 在这里看看-https: //www.w3schools.com/js/js_strings.asp

And decide the string format you want to work with. 并确定您要使用的字符串格式。

If you want to escape a character (which actually means to have it in your string), you should add \\ before this character. 如果要转义字符 (实际上意味着将其包含在字符串中),则应在此字符前添加\\

In my case, I just use String.raw() . 就我而言,我只使用String.raw()

JSON.parse(`{ "key": "\""}`) // fail
JSON.parse(String.raw`{ "key": "\""}`) // work

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

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