Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 问题:序列化JSON多维列表_C#_.net_Json_Multidimensional Array_Json.net - Fatal编程技术网

C# 问题:序列化JSON多维列表

C# 问题:序列化JSON多维列表,c#,.net,json,multidimensional-array,json.net,C#,.net,Json,Multidimensional Array,Json.net,我正在使用JSON.Net序列化多维列表,但在使其正确序列化时遇到问题 以下是我期望的结果: "children": [ { "children": [ { "children": [ { "date_added": "12995911618983970", "id": "8", "name": "NestedNestedURL", "t

我正在使用JSON.Net序列化多维列表,但在使其正确序列化时遇到问题

以下是我期望的结果:

     "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"; }
        }
    }
那么,如何更改代码以匹配输入和输出?就像我说的,似乎是多个文件夹没有应用到一个父文件夹


编辑:我上传了完整版本的代码,因为我由于空间的原因删掉了一些部分。

首先分配
folder.children=(setChild(fullpathlist,folder,mark,1))和几行之后,您将用
folder.children=(u)覆盖它。您的文件夹对象中不会有递归调用的子对象。

我不知道这是否是您想要的:

/// <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);
    }
}

我曾经生成类,没有阅读文档,只是传递了我的书签,所以有可能缺少一些字段

你从什么结构开始?再看看你贴在这里的代码。它不完整,无法编译。@Jeffmerca我是否为了节省空间而删减了代码,但如果您愿意,我可以将其放在粘贴箱中或将整个内容发布?请再看一次。您有两个签名完全相同的
Sync()
方法。第一个甚至不完整。在<<代码>中,对于循环,它似乎被切断了。“不可能知道它应该做什么。”杰夫梅尔卡多·伊克斯。我不知道这是怎么发生的。很抱歉。下面是我的完整代码:为什么要将子对象声明为
公共对象子对象{get;set;}
。它应该是
公共列表子项{get;set;}
/// <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; }
}