Reputation: 950
I have this kind of format for the Json data:
{
"Name": "MainMenu",
"Text": "",
"Guid": "047bf56e75f44c14b56a4b84f3af6dbe",
"ParentGuid": "00000000000000000000000000000000"
"MenuEntries": [{
"Name": "Home",
"Text": "Home",
"Guid": "1e3df5fb3d8e463b8f37e8ccdb291802",
"ParentGuid": "047bf56e75f44c14b56a4b84f3af6dbe",
"MenuEntries": [{
"Name": "About",
"Text": "About",
"Guid": "f476a3444e2943668d86433f4c88374c",
"ParentGuid": "1e3df5fb3d8e463b8f37e8ccdb291802",
"MenuEntries": []
}, {
"Name": "Contact",
"Text": "Contact Us",
"Guid": "51b461ec6abd4aecb95cf662e8e15ae6",
"ParentGuid": "1e3df5fb3d8e463b8f37e8ccdb291802",
"MenuEntries": []
}
]
}, {
"Name": "About",
"Text": "About",
"Guid": "6954d6f747ef4d2dae33de0f0efae9cc",
"ParentGuid": "047bf56e75f44c14b56a4b84f3af6dbe",
"MenuEntries": []
}, {
"Name": "Contact",
"Text": "Contact Us",
"Guid": "634cfb99b70b4395b94061ae4f082aba",
"ParentGuid": "047bf56e75f44c14b56a4b84f3af6dbe",
"MenuEntries": []
}
]
}
How to find a specific node through Linq using the 'Guid' field for filtering?
Also how to add a child node('MenuEntries') in the selected node and delete this node?
Upvotes: 2
Views: 74
Reputation: 3012
You can create a Model :
public class MenuEntry
{
public string Name { get; set; }
public string Text { get; set; }
public string Guid { get; set; }
public string ParentGuid { get; set; }
public List<object> MenuEntries { get; set; }
}
public class RootObject
{
public string Name { get; set; }
public string Text { get; set; }
public string Guid { get; set; }
public string ParentGuid { get; set; }
public List<MenuEntry> MenuEntries { get; set; }
}
And deserialize it :
string jsonString = System.IO.File.ReadAllText(Server.MapPath("~/json/test.json"));
RootObject Menu = JsonConvert.DeserializeObject<RootObject>(jsonString);
You can then select the node in MenuEntries like that :
foreach (var item in Menu.MenuEntries)
{
if (item.Guid == "1e3df5fb3d8e463b8f37e8ccdb291802")
{
Console.WriteLine(item.Name);
//=> Home
}
}
Upvotes: 1