消毒Chrome JSON书签

时间:2012-10-29 22:19:31

标签: c# json google-chrome json.net

我正在尝试以编程方式将书签添加到Chrome的书签文件中。它以JSON格式存储,我使用JSON.Net对其进行去除。 Here是我用来消毒它的代码,如果有帮助的话。读取每个书签后,它将存储在我的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可以正确读取书签文件。

谢谢!

0 个答案:

没有答案