JSON.NET和替换@登录XML以进行JSON转换 [英] JSON.NET and Replacing @ Sign in XML to JSON converstion
本文介绍了JSON.NET和替换@登录XML以进行JSON转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JSON.NET框架可以将XML转换为JSON,但是它使用JSON中的@符号作为属性.我宁愿在将其发送到视图之前将其删除.最好的方法是什么?
The JSON.NET framework can convert XML to JSON, but it uses the @ sign in the JSON as the attribute. I would rather remove this before sending it to the view. What would be the best approach for this?
我知道我可以直接进行替换,但是@字符可能与某个地方相关,因此不应替换.有正则表达式吗?
I know I can do a straight up replace, but an @ character may be relevant somewhere and shouldn't be replaced. Is there a Regex for this?
public ActionResult Layout()
{
var xml = new XmlDocument();
xml.XmlResolver = null;
xml.Load(Server.MapPath("~/App_Data/Navigation.xml"));
return Content(JsonConvert.SerializeXmlNode(xml, Newtonsoft.Json.Formatting.Indented));
}
{
"Layout": {
"Navigation": [
{
"@Type": "Menu",
"@Title": "Dashboard"
},
{
"@Type": "Menu",
"@Route": "Events",
"@Title": "Events",
"Navigation": {
"@Type": "Action",
"@Route": "Event",
"@Title": "+ Add Event",
"@Order": "1",
"Navigation": {
"@Type": "Item",
"@Route": "Event",
"@Name": "Event",
"Navigation": [
{
"@Route": "Pools",
"@Type": "SubNavigation",
"@Name": "Pools"
},
{
"@Route": "Brackets",
"@Type": "SubNavigation",
"@Name": "Brackets"
}
]
}
}
}
]
}
}
推荐答案
我继续使用了它.让我知道是否有更好的方法.
I went ahead and used this. Let me know if there is a better way.
public ActionResult Layout()
{
var xml = new XmlDocument();
xml.XmlResolver = null;
xml.Load(Server.MapPath("~/App_Data/Navigation.xml"));
var jsonText = JsonConvert.SerializeXmlNode(xml, Newtonsoft.Json.Formatting.Indented);
return Content(Regex.Replace(jsonText, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase));
}
这篇关于JSON.NET和替换@登录XML以进行JSON转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文