简体   繁体   English

ASP.NET Web API 2 - 序列化后字符串中的双反斜杠

[英]ASP.NET Web API 2 - double backslash in string after serialization

I'm dealing with issue of backslashes in the string. 我正在处理字符串中的反斜杠问题。

I have method like this 我有这样的方法

public IHttpActionResult GetResult()
{
    return Ok(@"\");
}

But after JSON serialization I get this result in http response 但是在JSON序列化之后,我在http响应中得到了这个结果

"\\\\" “\\\\”

Is there any possibility to disable adding backslash during serialization? 是否有可能在序列化期间禁用添加反斜杠? I know that I can do it by replacing \\\\ with \\ before response but it is not elegant way for me. 我知道我可以通过在响应之前将\\替换为\\来实现它,但这对我来说并不优雅。

You can't disable adding a backslash before a \\ because it wouldn't be valid JSON (see here ). 您无法在\\之前禁用添加反斜杠,因为它不是有效的JSON(请参阅此处 )。 The backaslash will always be added in the following cases: 在以下情况下将始终添加反斜杠:

在此输入图像描述

But once your JSON is deserialized you should only get a single backslash for every \\\\ in your JSON string. 但是一旦你的JSON被反序列化,你应该只为你的JSON字符串中的每个\\\\获得一个反斜杠。

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

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