c# json 反序列化复杂结构

问题描述 投票:0回答:1

我有一个像这样的json文档:

[
    {
        "TimerDuration": "1",
        "UpdateDate": "a",
        "DataBinding": [
            {
                "Name": "a",
                "Path": "b"
            },
            {
                "Name": "a",
                "Path": "b"
            }
        ]
    }
]

这背后的类是:

internal class Settings
{
    public String TimerDuration;
    public String UpdateDate;
    public List<DataBindings> DataBinding = new List<DataBindings>();
}

和:

internal class DataBindings
{
    public String Name;
    public String Path;
}

我想将文档反序列化成字符串,我可以使用。我该怎么做?

c# json json-deserialization complex-data-types
1个回答
0
投票

JSON 中的对象列表为 [] 意味着作为示例(使用 Newtonsoft),还请更具体说明您的意思是 [将文档反序列化为字符串]???

void Main()
{
    string json = "[{\"TimerDuration\":\"1\",\"UpdateDate\":\"a\",\"DataBinding\":[{\"Name\":\"a\",\"Path\":\"b\"},{\"Name\":\"a\",\"Path\":\"b\"}]}]";
    
    JsonConvert.DeserializeObject<List<Settings>>(json).Dump();
    
    
}


internal class Settings
{
    public String TimerDuration;
    public String UpdateDate;
    public List<DataBindings> DataBinding = new List<DataBindings>();
}

internal class DataBindings
{
    public String Name;
    public String Path;
}

© www.soinside.com 2019 - 2024. All rights reserved.