Json.Net Deserailization

本文关键字:Deserailization Net Json | 更新日期: 2023-09-27 18:02:33

是否有可能像这样反序列化一个结构?

{
    "name": "Peter Pan",
    "entries": [
        [
            "x",
            {
                "path": "/The Missing Picture/Blog",
            }
        ],
        [
            "y",
            {
                "path": "/the missing picture/Blog/Transformation-Copy.txt",
            }
        ]            
    ],
    "reset": true
}

到下面的类

public class Person
{
    public String Name {get;set;}
    public IDictionary<String, Entry> Entries {get;set;}
}
public class Entry
{
    public String Path {get;set;}
}

Json.Net Deserailization

不直接对齐,不

使用json2sharp,我们可以看到JSON的等效类结构

public class RootObject
{
    public string name { get; set; }
    public List<List<object>> entries { get; set; }
    public bool reset { get; set; }
}

您可以反序列化为该结构的对象,然后使用LINQ表达式将列表的列表转换为字典。

如果你想匹配类结构,你需要JSON看起来像这样:

{
    "name": "Peter Pan",
    "entries": [
        {
            "x" :
            {
                "path": "/The Missing Picture/Blog",
            }
        },
        {
            "y" :
            {
                "path": "/the missing picture/Blog/Transformation-Copy.txt",
            }
        }
    ],
    "reset": true
}