如何从C#中的json数据中删除反斜杠? [英] How to remove backslash from json data in C#?

查看:552
本文介绍了如何从C#中的json数据中删除反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从以下json数据中删除反斜杠

{\filed\:\0d5bd590-86b5-4281-9ef7-924c15190273 \,\ FirstName \:\1sssqwer \,\LastName \:\sss2 \,\EmailId \:\ssss3@infor.com \,\\ \\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.com\", \"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数据中删除反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆