通过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);

}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-10-11
  • 2021-10-12
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案