简体   繁体   English

C#序列化JSON将\\变成\\\\\\\\

[英]C# Serialize JSON turn \ into \\\\

I am generating JSON for inclusion direction into a HTML file and as such the JSON gets wrapped in a Javascript String. 我正在生成JSON以将其包含在HTML文件中,因此JSON被包装在Javascript字符串中。

eg 例如

var dataContacts = 
        '{"Contacts":[{"Id":0,"Active":false,"Company":"Rory The Architect\\, Melt"}]}';

var contacts = $.parseJSON(dataContacts).Contacts;

This code FAILS in javascript because it should be 此代码在javascript中失败,因为它应该是

var dataContacts = 
        '{"Contacts":[{"Id":0,"Active":false,"Company":"Rory The Architect\\\\, Melt"}]}';

var contacts = $.parseJSON(dataContacts).Contacts;

I've used both .NET Serializer and NewtonSoft.Json and neither have support for turning a \\ into a \\\\\\\\, they do offcourse turn the \\ into a \\\\ which would be fine for AJAX delivery of JSON but not for JSON embbeded in a Javascript String 我曾经使用过.NET Serializer和NewtonSoft.Json,但都没有将\\变成\\\\\\\\的支持,他们确实会偏离路线将\\变成\\\\,这对于AJAX传递JSON很好,但对于JSON则没有问题。嵌入到Javascript字符串中

I assume I may need to do some sort of myJson.Replace("\\", "\\\\"); 我假设我可能需要做一些myJson.Replace(“ \\”,“ \\\\”);

I've looked at why I need a \\\\ in the output, see this link here: 我已经查看了为什么在输出中需要\\\\,在这里查看此链接:

Why does the jQuery JSON parser need double escaping for backslashes? 为什么jQuery JSON解析器需要对反斜杠进行两次转义?

The first escape escapes it in the Javascript string literal. 第一个转义符使用Javascript字符串文字对其进行转义。 The second escape escapes it in the JSON string literal. 第二个转义以JSON字符串文字形式对其进行转义。

The Javascript expression '{"a":"b:\\c"}' evaluates to the string '{"a":"b:\\c"}'. Javascript表达式'{“ a”:“ b:\\ c”}'的计算结果为字符串'{“ a”:“ b:\\ c”}'。 This string contains a single unescaped \\, which must be escaped for JSON. 该字符串包含一个未转义的\\,对于JSON必须转义。 In order to get a string containing \\, each \\ must be escaped in the Javascript expression, resulting in "\\\\". 为了获得包含\\的字符串,必须在Javascript表达式中对每个\\进行转义,以产生“ \\\\”。

I am wondering the best path forward on this issue and why neither NewtonSoft or the .NET serializer have support for encoding JSON for inclusion directly into the Javascript file 我想知道关于此问题的最佳途径,以及为什么NewtonSoft或.NET序列化器都不支持将JSON编码以直接包含在Javascript文件中

It looks to me like you're causing yourself undue trouble by doing things the hard way. 在我看来,您正在以艰难的方式做事给自己造成不必要的麻烦。 Why are you putting JSON into a JavaScript string just so you can immedately parse it? 为什么将JSON放到JavaScript字符串中只是为了立即解析它? The JSON is already JavaScript, so why not just use it? JSON已经是JavaScript,那么为什么不使用它呢?

var dataContacts = 
       {"Contacts":[{"Id":0,"Active":false,"Company":"Rory The Architect\\, Melt"}]};

var contacts = dataContacts.Contacts;

And as Paul Draper points out, the approach you're tring to use provides all sort of extra complications once apostrophes and other characters start getting involved. 就像Paul Draper指出的那样,一旦撇号和其他字符开始涉及到您,您要使用的方法会带来各种额外的复杂性。

There is actually a second problem here as well, which you have not identified: What if the JSON includes a single quote ( ' ), eg Rory's Architect ? 实际上,这里还有第二个问题,您还没有发现:如果JSON包含单引号( ' ),例如Rory's Architect怎么办?

Not only with the Javscript string not be valid JSON, it won't even be valid Javascript ! 不仅Javscript字符串不是有效的JSON,它甚至也不是有效的Javascript

var dataContacts = 
    '{"Contacts":[{"Id":0,"Active":false,"Company":"Rory's Architect\\, Melt"}]}';

To resolve this, you can do (in C#) 要解决此问题,您可以执行(在C#中)

string myJsonString = myJson.Replace("\\", "\\\\").Replace("'", "\\'")

Note that the escaping the backslashes was necessary in C# to get \\ , \\\\ , and \\' . 请注意,在C#中转义反斜杠对于获取\\\\\\\\'是必需\\' It's a lot of escaping, all around ;) 到处都是逃避;)

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

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