2012-10-28 44 views
4

我采用序列JSON.Net多维列表,我有使其正确序列化的问题。发行序列化JSON多维名单

下面是结果我从这个预期:

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

然而,这比结果我真正得到很大的不同。这里的问题,我认为,这是多重的URL可以属于一个单一的文件夹,多个文件夹到一个文件夹,但我不确定如何实现这一点。这是我目前使用的代码:

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

这里是我的输出从该代码得到:

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

那么,怎样才能更改我的代码相匹配的输入和输出?就像我说的,它似乎是关于多个文件夹没有被应用到一个父文件夹的东西。

编辑:我已经上传完整版本的代码here的,因为我切出由于空间的某些部分。

+0

什么是你先从结构?再看看你在这里粘贴的代码。它不完整,不会编译。 –

+0

@JeffMercado我上剪下来的空间用途的代码,但我可以把它放在一个引擎收录或张贴整个事情,如果你喜欢? –

+0

再看一遍。你有两个'Sync()'方法具有相同的确切签名。其中第一个甚至不是完整的。它看起来像是在'for'循环中间被切断了。不可能知道它应该做什么。 –

回答

3

我不知道这是否是你想要什么:

/// <summary> 
/// Load file from 
/// C:\Users\XXXX\AppData\Local\Google\Chrome\User Data\Default\Bookmarks 
/// </summary> 
[TestFixture] 
public class ChromeBookmarks 
{ 
    [Test] 
    public void RoundTripBookmarksTest() 
    { 
     JsonSerializer jsonSerializer = JsonSerializer.Create(
      new JsonSerializerSettings 
       { 
        MissingMemberHandling = MissingMemberHandling.Error, 
       }); 
     RootObject rootObject; 
     using (JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(Properties.Resources.Bookmarks))) //I added my bookmars as an embedded txt file for convenience 
     { 
      rootObject = jsonSerializer.Deserialize<RootObject>(jsonTextReader); 
     } 
     //Here you have the contents of the bookmarks file in rootObject if you need to do manipulations 
     StringBuilder stringBuilder = new StringBuilder(); 
     using (JsonTextWriter jsonTextWriter = new JsonTextWriter(new StringWriter(stringBuilder))) 
     { 
      jsonTextWriter.Formatting = Formatting.Indented; 
      jsonTextWriter.Indentation = 3; 
      jsonSerializer.Serialize(jsonTextWriter, rootObject); 
     } 
     var json = stringBuilder.ToString(); 
     File.WriteAllText(@"C:\person.json", json); 
    } 
} 
public class Child 
{ 
    [JsonProperty(PropertyName = "children")] 
    public List<Child> Children { get; set; } 
    [JsonProperty(PropertyName = "date_added")] 
    public string DateAdded { get; set; } 
    [JsonProperty(PropertyName = "id")] 
    public string Id { get; set; } 
    [JsonProperty(PropertyName = "name")] 
    public string Name { get; set; } 
    [JsonProperty(PropertyName = "type")] 
    public string Type { get; set; } 
    [JsonProperty(PropertyName = "url")] 
    public string Url { get; set; } 
    [JsonProperty(PropertyName = "date_modified")] 
    public string DateModified { get; set; } 
} 
public class Roots 
{ 
    [JsonProperty(PropertyName = "bookmark_bar")] 
    public Child BookmarkBar { get; set; } 
    [JsonProperty(PropertyName = "other")] 
    public Child Other { get; set; } 
    [JsonProperty(PropertyName = "synced")] 
    public Child Synced { get; set; } 
} 
public class RootObject 
{ 
    [JsonProperty(PropertyName = "checksum")] 
    public string Checksum { get; set; } 
    [JsonProperty(PropertyName = "roots")] 
    public Roots Roots { get; set; } 
    [JsonProperty(PropertyName = "version")] 
    public int Version { get; set; } 
} 

我以前this生成的类,没读过文档只是通过我的书签,所以有可能丢失一些字段。

3

首先分配folder.children = (setChild(fullpathlist, folder, mark, 1));和几行后,你覆盖现有folder.children=(u);。你不会让你的文件夹对象中的孩子进行递归调用。

+0

好的,我会纠正我的代码。 –