使用数组数组将Json反序列化为C#对象

时间:2017-02-21 07:58:11

标签: c# arrays json

您好我将JSON文件填充到C#对象时遇到了严重问题,因为JSON包含"数组数组"。我需要创建具有所有数组的类,以便在反序列化后能够加载文件,但我不知道如何创建这样的结构。

JSON文件:

{
    "status": true,
    "result": [
        {
            "ID": "1",
            "UserID": "1001",
            "web_id": "2753",
            "certificate_id": "31",
            "domain": "testsomething.com",
            "status": "ssl_uninstalling",
            "updated_at": "2017-02-15 15:08:04",
            "IP": "xx.x.xxx.xxx",
            "OS_version": "CentOS release 4.8"
        },
        {
            "ID": "2",
            "UserID": "1001",
            "web_id": "2753",
            "certificate_id": "31",
            "domain": "admin.testsomething.com",
            "status": "ssl_uninstalling",
            "updated_at": "2017-02-13 14:19:45",
            "IP": "xx.x.xxx.xxx",
            "OS_version": "CentOS release 4.8"
        },
        {
            "ID": "3",
            "UserID": "1001",
            "web_id": "2753",
            "certificate_id": "31",
            "domain": "www.testsomething16.com",
            "status": "ssl_uninstalling",
            "updated_at": "2017-02-16 16:22:40",
            "IP": "xx.x.xxx.xxx",
            "OS_version": "CentOS release 4.8"
        },
        {
            "ID": "4",
            "UserID": "1001",
            "web_id": "2753",
            "certificate_id": "31",
            "domain": "blog.testsomething.com",
            "status": "ssl_uninstalling",
            "updated_at": "2017-02-16 16:22:40",
            "IP": "xx.x.xxx.xxx",
            "OS_version": "CentOS release 4.8"
        }
    ],
    "errors": []
}

2 个答案:

答案 0 :(得分:1)

json的结构可能看起来像

public class Result
{

    [JsonProperty("ID")]
    public string ID { get; set; }

    [JsonProperty("UserID")]
    public string UserID { get; set; }

    [JsonProperty("web_id")]
    public string web_id { get; set; }

    [JsonProperty("certificate_id")]
    public string certificate_id { get; set; }

    [JsonProperty("domain")]
    public string domain { get; set; }

    [JsonProperty("status")]
    public string status { get; set; }

    [JsonProperty("updated_at")]
    public string updated_at { get; set; }

    [JsonProperty("IP")]
    public string IP { get; set; }

    [JsonProperty("OS_version")]
    public string OS_version { get; set; }
}

public class Rootobject
{

    [JsonProperty("status")]
    public bool status { get; set; }

    [JsonProperty("result")]
    public List<Result> result { get; set; }

    [JsonProperty("errors")]
    public List<object> errors { get; set; }
}

您可以使用Visual Studio为JSON创建类。 将您的JSON复制到剪贴板中。这意味着只需 CTRL + C 然后转到Visual Studio Edit - &gt;选择性粘贴 - &gt;将JSON粘贴为类

为JSON创建类结构的另一种方法是,您可以转到json2csharp: generate c# classes from json并创建类。

答案 1 :(得分:0)

我认为您可以创建如下模型:

public class Example
    {
        public bool Status { get; set; }
        public ResultModel ResultModel { get; set; }
        public bool Error { get; set; }

        public Example()
        {
            ResultModel = new ResultModel();
        }
    }

    public class ResultModel
    {
        public int ID { get; set; }
        public int UserId { get; set; }
        public int WebId { get; set; }
    }