1

我正在尝试以编程方式将书签添加到 Chrome 的书签文件中。它以 JSON 格式存储,我正在使用 JSON.Net 对其进行去序列化。如果有帮助,这是我用来对其进行消毒的代码。阅读每个书签后,它都会存储在我的 Bookmark 类中。

public class Bookmark
{
    public string url { get; set; }
    public string title { get; set; }
    public string browser { get; set; }
    public DateTime added { get; set; }
    public string fullPath { get; set; }
    public object currentPath { get; set; }
    public Bookmark(string url, string title, string browser, DateTime added, string fullPath, object currentPath = null)
    {
        this.url = url;
        this.title = title;
        this.browser = browser;
        this.added = added;
        this.fullPath = fullPath;
        this.currentPath = currentPath;
    }

现在,当我尝试对书签进行消毒并将它们存储在 Chrome 的书签文件中时,问题就来了。当我尝试对此进行消毒时,我遇到了列表问题,因为在我的课堂上,每个文件夹只能有一个 URL 或文件夹作为子文件夹,但是,我不确定你是如何实现的。我还相信,为了使其正常工作,您必须有某种方法检查当前文件夹并查看它是否与上一个文件夹相同,而且我不确定这可能如何工作。

这是预期的输出:

"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"
 }

这是我使用当前代码收到的错误输出:

"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"
  }

我正在使用此代码:所有类都用于 JSON 灭菌。就像我说的,我相信我的 Folder 类必须改变一些东西。

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 = (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=(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;
        }            
    }

        class json_root
        {
            public Folder bookmark_bar { get; set; }
            public Folder other { get; set; }
        }

        class json_top
        {
           public string checksum { get { return ""; } }
           public json_root roots { get; set; }
            public int version { get { return 1; } }
        }

        class json_URL : folderAndURL
        {
            public long date_added { get; set; }
            public int id { get; set; }
            public string name { get; set; }
            public string type { get { return "url"; } }
            public string url { get; set; }
        }

    public class Folder
    {
        public object 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"; }
        }
    }

我需要帮助以使我的输出与我的输入相匹配,以便 Chrome 可以正确读取书签文件。

谢谢!

4

0 回答 0