简体   繁体   English

使用转义引号将只读字符串解析为JSON

[英]Parse readonly string with escaping quotes to JSON

The title of my question can be somewhat confusing, so I'll try to explain my situation: 我的问题的标题可能有些令人困惑,因此我将尝试解释我的情况:

From an external source (in this case a Web Socket connection), I receive a JSON string that needs to be parsed to an object in JavaScript. 从外部源(在本例中为Web套接字连接),我收到一个JSON字符串,该字符串需要解析为JavaScript中的对象。 I do not have the possibility to modify the fashion how the string is built up in the external source. 无法修改在外部源中如何构建字符串的方式。 I know that there are several options to parse, like using jQuery, eval() (not always recommended), etc ... In this case, I just use JSON.parse() because it seems to work just fine. 我知道有几种解析选项,例如使用jQuery, eval() (并不总是推荐)等。在这种情况下,我只使用JSON.parse()因为它似乎工作得很好。

Today I ran into a problem with double quotes being escaped in the string. 今天,我遇到了一个问题,字符串中的双引号被转义了。 Take the following example: 请看以下示例:

var readonly = '{"nam":"jso\\"n"}'; var readonly ='{“ nam”:“ jso \\” n“}';

Suppose that readonly is the string that is received from the external source (as the name suggests, I can't modify how the string is formatted at this level). 假设readonly是从外部源接收到的字符串(顾名思义,我无法在此级别修改字符串的格式)。 This is actually valid JSON, to there's really no reason for me to complain to the developers of the external source that they should modify theirs. 这实际上是有效的JSON,实际上我没有理由向外部源的开发人员抱怨他们应该修改自己的JSON。 Now I would like to parse this string to an object. 现在,我想将此字符串解析为一个对象。 As already mentioned, I use JSON.parse() for this: 如前所述,我为此使用JSON.parse()

var my_object = JSON.parse(readonly); var my_object = JSON.parse(只读);

This unfortunately fails, and the browser's console throws an error message. 不幸的是,这失败了,浏览器的控制台抛出了一条错误消息。

After research, I found that I need to add another backslash to escape the double quotes in order for this to work. 经过研究,我发现我需要添加另一个反斜杠来转义双引号,以使其起作用。 So I need to find a way to change the readonly string into this: 所以我需要找到一种方法来将readonly字符串更改为此:

'{"nam":"jso\\\\"n"}' '{ “南”: “JSO \\\\” N“}'

My question now is: How can I add these extra backslashes to strings of which I don't know their content up front? 我现在的问题是:如何将这些多余的反斜杠添加到我不知道其内容的字符串中?

EDIT: Based on the comments, here's some extra info: I actually receive a string that contains more than just the json. 编辑:根据评论,这里有一些额外的信息:我实际上收到的字符串不仅包含json。 A fixed number of characters are in front an behind of the json. 固定数量的字符位于json的前面和后面。 This could be an example: 这可能是一个示例:

'xxxx{"nam":"jso\\"n"}xxxxxx' 'XXXX { “南”: “JSO \\” N“} XXXXXX'

I use substring to extract the actual json from this string. 我使用substring从该字符串中提取实际的json。

The string is incorrect - it needs to be double escaped. 字符串不正确-需要对其进行两次转义。

var readonly = '{"nam":"jso\\"n"}';
                           ^^

Remember that you're defining this string as a javascsript string, which means it's parsed/evaluated by Javascript **FIRST*. 请记住,您正在将该字符串定义为javascsript字符串,这意味着它是由Javascript ** FIRST *解析/评估的。 That means the characters literally stored in the var will simply be {"nam":"json"n"} - the JS parser will have removed the backslash. And now it's corrupted/invalid json. 这意味着从字面上存储在var中的字符将只是{"nam":"json"n"} -JS解析器将删除反斜杠。现在它已损坏/无效json。

By doubling up the backslashes, you ensure that ONE of the backslashes will survive and be present to make it valid JSON, which can then be parsed. 通过将反斜杠加倍,可以确保其中一个反斜杠可以保留并存在以使其成为有效的JSON,然后可以对其进行解析。

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

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