I am serializing a multidimensional list using JSON.Net and I am having trouble serializing it correctly.
Here is the result I expected from this:
"children": [ { "children": [ { "children": [ { "date_added": "12995911618983970", "id": "8", "name": "NestedNestedURL", "type": "url", "url": "http://url.com/" } ], "date_added": "12995911579845538", "date_modified": "12995911618983970", "id": "5", "name": "NestedNestedFolder", "type": "folder" }, { "date_added": "12995911609609970", "id": "7", "name": "NestedURL", "type": "url", "url": "http://url.com/" } ], "date_added": "12995911570603538", "date_modified": "12995911609609970", "id": "4", "name": "NestedFolder", "type": "folder" }, { "children": [ { "date_added": "12995911631570970", "id": "9", "name": "NestedURL2", "type": "url", "url": "http://url.com/" } ], "date_added": "12995911589790970", "date_modified": "12995911631570970", "id": "6", "name": "NestedFolder2", "type": "folder" }
However, this is very different from the result that I really get. I believe the problem is that multiple URLs may belong to the same folder and several folders in the same folder, but I'm not sure how to implement this. Here is the code I'm currently using:
public void Sync() { Browser browser = new Chrome(); SortableBindingList<Bookmark> marks = browser.ReturnBookmarks(); SortableBindingList<object> newmarks = new SortableBindingList<object>(); int count = 0; newmarks.Add(new json_top()); json_top top = newmarks[0] as json_top; top.roots = new json_root(); List<object> roots = new List<object>(); Folder bookmarks_bar = new Folder(); Folder other = new Folder(); List<Object> barlist = new List<Object>(); List<object> otherlist = new List<object>(); List<object> children = new List<object>(); List<string> existingFolders = new List<string>(); bookmarks_bar.date_added = ToChromeTime(marks[0].added); bookmarks_bar.name = "Bookmarks bar"; other.date_added = ToChromeTime(marks[0].added); other.name = "Other bookmarks"; bool isBookmarkBar = true; Folder folder = new Folder(); foreach (Bookmark mark in ordered) { List<string> fullpathlist = mark.fullPath.Split('\\').ToList(); int count1 = 0; if (currentPath != mark.fullPath) { folder = (createFolder(fullpathlist, fullpathlist[0], mark)); folder.children.Add(setChild(fullpathlist, folder, mark, 1)); count++; } json_URL u = new json_URL(); u.date_added = ToChromeTime(mark.added); u.id = id; u.name = mark.title; u.url = mark.url; folder.children.Add(u); if(isBookmarkBar) barlist.Add(folder); else { otherlist.Add(folder); } } bookmarks_bar.children = barlist; other.children = otherlist; top.roots.bookmark_bar = (bookmarks_bar); top.roots.other = other; string json = JsonConvert.SerializeObject(newmarks, Formatting.Indented); File.WriteAllText(@"c:\person.json", json); } Folder setChild(List<string> fullPathList, Folder parent, Bookmark mark, int count ) { if (count < fullPathList.Count) { Folder child = new Folder(); child.date_added = ToChromeTime(mark.added); child.id = id; child.name = fullPathList[count]; Folder LIST = setChild(fullPathList, child, mark, count + 1); child.children= LIST; return child; } } public class Folder { public List<folderAndURL> children { get; set; } public long date_added { get; set; } public long date_modified { get { return 0; } } public int id { get; set; } public string name { get; set; } public string type { get { return "folder"; } } }
And here is the result that I get from this code:
"children": [ { "children": { "date_added": 12995893609609970, "id": 0, "name": "NestedURL", "type": "url", "url": "http://url.com/" }, "date_added": 12995893609609970, "date_modified": 0, "id": 0, "name": "NestedFolder", "type": "folder" }, { "children": { "date_added": 12995893618983970, "id": 1, "name": "NestedNestedURL", "type": "url", "url": "http://url.com/" }, "date_added": 12995893618983970, "date_modified": 0, "id": 1, "name": "NestedFolder", "type": "folder" }, { "children": { "date_added": 12995893631570970, "id": 2, "name": "NestedURL2", "type": "url", "url": "http://url.com/" }, "date_added": 12995893631570970, "date_modified": 0, "id": 2, "name": "NestedFolder2", "type": "folder" }
So how can I change my code to match input and output? As I said, it looks like multiple folders are not applied to the same parent folder.
Edit: I downloaded the full version of the code here because I cut some parts due to a space.