如何从C#中的json数据中删除反斜杠?

编程入门 行业动态 更新时间:2024-10-14 06:20:03
本文介绍了如何从C#中的json数据中删除反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从以下json数据中删除反斜杠 {\filed\:\0d5bd590-86b5-4281-9ef7-924c15190273 \,\ FirstName \:\1sssqwer \,\LastName \:\sss2 \,\EmailId \:\ssss3@infor \,\\ \\ClientPrincipal \:\0d5bd590-86b5-4281-9ef7-924c15190273 \} 但不会删除 我尝试过: string s = @{[]}; var result = s; var data = JsonConvert.SerializeObject(result); string [] tokens = data.Split(','); userRole = userRole.Replace(\,);

I want to remove back slash fro the below json data { \"filed\": \"0d5bd590-86b5-4281-9ef7-924c15190273\", \"FirstName\": \"1sssqwer\", \"LastName\": \"sss2\", \"EmailId\": \"ssss3@infor\", \"ClientPrincipal\": \"0d5bd590-86b5-4281-9ef7-924c15190273\"} but that will not removed What I have tried: string s = @"{[]}"; var result = s; var data = JsonConvert.SerializeObject(result); string[] tokens = data.Split(','); userRole = userRole.Replace("\"", "");

推荐答案

答案很简单: 字符串中没有反斜杠! 你看到了什么是Visua l Studio调试器对字符串的表示。由于您使用的是C#,它会显示带有转义字符的字符串,您需要在C#代码中将该值键入为字符串文字。但是这些转义字符实际上并不是字符串的一部分。 如果使用即时窗口,请输入:?data,nq 你应该看到没有转义字符的字符串的内容。 The answer is simple: there is no backslash in the string! What you're seeing is the Visual Studio debugger's representation of the string. Since you're using C#, it displays the string with the escape characters you would need to include to type that value as a string literal in your C# code. But those escape characters are not actually part of the string. If you use the immediate window, type: ?data,nq and you should see the contents of the string without the escape characters.

方法1:使用string.replace Approach 1: use string.replace json.Replace(@"\", " ")

方法2:使用正则表达式。 Unescape

Approach 2: use Regex.Unescape

var regex = new Regex(pattern); data = Regex.Unescape(data);

谢谢理查德,我在VS调试中看到这个并没有注册它是VS做的。 让我感觉的是使用Chrome RESTLet客户端扩展程序测试我的REST服务结果,同时也显示了来自我的服务的响应。 尝试过YARC扩展,结果不包括\ 奇怪! Thanks Richard, I was seeing this in VS debug and didn't register it was VS doing it. The thing that threw me was testing my REST service results using Chrome RESTLet client extension was showing responses from my service with the \ as well. Tried YARC extension and that result did not include the \ strange!

更多推荐

如何从C#中的json数据中删除反斜杠?

本文发布于:2023-11-06 08:15:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563165.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   数据   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!