转换JSON表对象数组

时间:2019-08-19 11:09:12

标签: c# .net json

我收到了一个来自json表对象数组的API的响应。

我正在尝试反序列化它,但是它总是返回null。

Json格式:

        {
            "tables": [
                {
                "name": "PrimaryResult",
                "columns": [
                    {
                    "name": "data1",
                    "type": "string"
                    },
                    {
                    "name": "data2",
                    "type": "string"
                    },
                    {
                    "name": "data3",
                    "type": "string"
                    }
                ],
                "rows": [
                    [
                    "9e94c46f-81b5-4186-8f25-42db447c94bb",
                    "value1",
                    "value2"
                    ],
                    [
                    "kds73j39dsd9sdsd0dd08d6d3dfdf443f44f4",
                    "value2",
                    "value3"
                    ]
                ]
                }
            ]
        }

我的课程:

        class DataConvert
        {
            public Data[] data { get; set; }
        }

        public class Data
        {
            public string data1 { get; set; }
            public string daat2 { get; set; }
            public string data3 { get; set; }
        }

        DataConvert data = JsonConvert.DeserializeObject<DataConvert>(json);

构建我的对象是否应该有所不同?

2 个答案:

答案 0 :(得分:1)

public class Column
{
    public string name { get; set; }
    public string type { get; set; }
}

public class Table
{
    public string name { get; set; }
    public List<Column> columns { get; set; }
    public List<List<string>> rows { get; set; }
}

public class RootObject
{
    public List<Table> tables { get; set; }
}

为了使您的生活更轻松,只需使用http://json2csharp.com/从json生成c#类

答案 1 :(得分:0)

应该像

public class Rootobject
{
    public Table[] tables { get; set; }
}

public class Table
{
    public string name { get; set; }
    public Column[] columns { get; set; }
    public string[][] rows { get; set; }
}

public class Column
{
    public string name { get; set; }
    public string type { get; set; }
}

只需复制您的JSON字符串,转到visual studio编辑->过去的特殊->并选择过去的JSON作为类

您的对象将是

 Rootobject data = JsonConvert.DeserializeObject<Rootobject>(json);