C#去掉xml转成json后key里的@符号

通过Json.net把xml转换成json的时候,会把xml里的所有属性名前面加个@符号

参考:http://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm

Attributes are prefixed with an @ and should be at the start of the object.

但这样貌似就不能直接Deserialize<>成实体对象。

可以用一个正则表达式搞定。

Regex reg = new Regex(""@([^ "]*)"\s*:\s*"(([^ "]+\s*)*)"");

关键在于,只去掉key里面的@,而value里的@能保留下来。

如果value里能确保没有@,用string的Replace就可以啦。

demo代码

public static string RemoveAt(string json)
{
Regex reg = new Regex(""@([^ "]*)"\s*:\s*"(([^ "]+\s*)*)"");
string strPatten = ""$1":"$2"";
return reg.Replace(json, strPatten);

}

原文地址:https://www.cnblogs.com/imoonstal/p/6531908.html