Answered by:
Iterate through JSON and update database

Question
-
User-1020839931 posted
Hi,
I am trying to implement juqery plugin of Drag & drop hierarchical list where we can change the order of list items and also change parent menu of list items, below is a demo link:
https://dbushell.com/Nestable/
After drag & drop changes, it generates "Serialised JSON" output like below, where id is a MenuID used in a database:
[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6},{"id":7},{"id":8}]},{"id":9},{"id":10}]},{"id":11},{"id":12}]
Now, I want to save these changes into the database.
I have a Menu table with below columns:
MenuID, ParentID, MenuName, OrderNo
How can I loop through the above Serialised JSON output so that I can update ParentID and OrderNo for menu/sub-menu items?
Tuesday, March 6, 2018 6:54 AM
Answers
-
User-1838255255 posted
Hi jacob1290,
According to your description, as far as i know, you could check the following tutorial about how to Iterating through a nested JSON Array, then you could get the value and update the database.
Iterating through a nested JSON Array in C# with Newtonsoft:
C# Parsing JSON array of objects:
https://stackoverflow.com/questions/19910476/c-sharp-parsing-json-array-of-objects
Best Regards,
Eric Du
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 7, 2018 6:24 AM
All replies
-
User-1838255255 posted
Hi jacob1290,
According to your description, as far as i know, you could check the following tutorial about how to Iterating through a nested JSON Array, then you could get the value and update the database.
Iterating through a nested JSON Array in C# with Newtonsoft:
C# Parsing JSON array of objects:
https://stackoverflow.com/questions/19910476/c-sharp-parsing-json-array-of-objects
Best Regards,
Eric Du
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 7, 2018 6:24 AM -
User-1020839931 posted
Hi,
Thanks for links, I tried and its working code is below:
string json = "[{\"id\":1},{\"id\":2,\"children\":[{\"id\":3},{\"id\":4},{\"id\":5,\"children\":[{\"id\":6},{\"id\":7},{\"id\":8}]},{\"id\":9},{\"id\":10}]},{\"id\":11},{\"id\":12}]"; JArray obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JArray>(json); string menuID, subMenuID, subSubMenuID; foreach (var mainMenu in obj) { menuID = (string)mainMenu["id"]; if (mainMenu["children"] != null) { foreach (JObject subMenu in mainMenu["children"]) { subMenuID = (string)subMenu["id"]; if (subMenu["children"] != null) { foreach (JObject nestedMenu in subMenu["children"]) { subSubMenuID = (string)nestedMenu["id"]; } } } } }
Should I make any changes to the above code considering performance?
Wednesday, March 7, 2018 7:39 PM -
User-1838255255 posted
Hi jacob1290,
According to your description and code, i checked your code logical, i think the code logical is good, it is unnecessary to make change.
Best Regards,
Eric Du
Thursday, March 8, 2018 2:14 AM -
User-1020839931 posted
Thanks Eric.
Your links pointed me in right direction.
Thursday, March 8, 2018 11:41 AM