JSON.NET框架可以將XML轉換爲JSON,但它使用JSON中的@符號作爲屬性。我寧願刪除它之前發送到視圖。對此最好的方法是什麼?JSON.NET和替換@登錄XML到JSON對話
我知道我可以直接替換,但@字符可能在某處相關,不應該被替換。有這樣的正則表達式嗎?
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"
}
]
}
}
}
]
}
}
F你知道,那是@符號將在JSON出現的唯一場所,工作與string.replace更快或更大的文件 – andryuha
正如@yieio提到的,這仍然不適合工作以'@'開頭的值,例如' '會產生'{「test」:{「attr」:「foo」}}'。 –
Aristoteles