C# API调用后无法反序列化当前JSON数组

C# API调用后无法反序列化当前JSON数组,c#,json,asp.net-mvc,asp.net-core,C#,Json,Asp.net Mvc,Asp.net Core,我有一个NET Core MVC web应用程序,在其中调用API以在UI中显示结果。我使用ajax调用调用API,但调用API后,我收到以下错误消息: 处理请求时发生未处理的异常。 JsonSerializationException:无法将当前JSON数组(例如[1,2,3])反序列化为类型“ProjectName.Models.ControllerName”,因为该类型需要JSON对象(例如{name:value})才能正确反序列化 来自API的JSON结果: [ [

我有一个NET Core MVC web应用程序,在其中调用API以在UI中显示结果。我使用ajax调用调用API,但调用API后,我收到以下错误消息:

处理请求时发生未处理的异常。 JsonSerializationException:无法将当前JSON数组(例如[1,2,3])反序列化为类型“ProjectName.Models.ControllerName”,因为该类型需要JSON对象(例如{name:value})才能正确反序列化

来自API的JSON结果:

[
    [
        {
            "Col1": "Value",
            "Col2": "Value",
            "Col3": "Value",
            "Col4": [
                {
                    "Value": [
                        {}
                    ]
                },
                {
                    "Value": [
                        {}
                    ]
                }
            ],
            "Col5": "Value"
        }
    ],
    [
        {
            "Col1": "Value",
            "Col2": "Value",
            "Col3": "Value",
            "Col5": "Value"
         }
     ],
     [
        {
            "Col1": "Value",
            "Col2": "Value",
            "Col3": "Value",
            "Col4": [
                {
                    "Value": [
                        {}
                    ]
                },
                {
                    "Value": [
                        {}
                    ]
                 }
             ],
             "Col5": "Value"
        }
    ]
]
霉菌控制者

[HttpGet, ActionName("GetData")]
public async Task<IActionResult> GetReport()
{
    return this.Json(await _cuRepo.GetReportAsync("https://apiurlforcall/report"));
}
存储库

public async Task<IEnumerable<T>> GetReportAsync(string url)
{
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Token", "apiToken");
        HttpResponseMessage response = await client.GetAsync(url);
        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            //jsonString is populated with the above JSON result
            var jsonString = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<IEnumerable<T>>(jsonString);
        }
        else
            return null;
    }
}
AjaxCall我想做的事

$.ajax({
    url: "Controller/GetData",
    data: {},
    method: "GET",
    success: function (data) {
        var json = data;
        var html = "";

        for (var x = 0; x < json.length; x++) {
            html += "<tr><td>" + json[x].Col1+ "</td><td>" + json[x].Col2+ "</td></tr>";
        }
        $('#MyTable').html("");
        $('#MyTable').html(html);
        console.log(json);
    }
});
老实说,我对net core没有太多的经验,我正在尝试通过做这个项目来学习,但这个问题是一个很大的障碍,我有点迷路了。

你可以这样做:

型号:

public class Cols
    {
        public string  Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public List<Col4> Col4 { get; set; }
        public string Col5 { get; set; }

        
    }
    public class Col4
    {
        public List<Col> Value { get; set; }
    }
    public class Col
    { 

    }

3.改变

html += "<tr><td>" + json[x].Col1+ "</td><td>" + json[x].Col2+ "</td></tr>";

您可以这样做:

型号:

public class Cols
    {
        public string  Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public List<Col4> Col4 { get; set; }
        public string Col5 { get; set; }

        
    }
    public class Col4
    {
        public List<Col> Value { get; set; }
    }
    public class Col
    { 

    }

3.改变

html += "<tr><td>" + json[x].Col1+ "</td><td>" + json[x].Col2+ "</td></tr>";


您可以使用下面的类来反序列化json:

public class DataObject
    {
        public ColDetails[][] MyData { get; set; }
    }

    public class ColDetails
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public Col4[] Col4 { get; set; }
        public string Col5 { get; set; }
    }

    public class Col4
    {
        public Value[] Value { get; set; }
    }

    public class Value
    {
    }
然后,在转换到类后返回所需的对象

使用以下代码进行反序列化:

JsonConvert.DeserializeObject<DataObject>(jsonString);

您可以使用下面的类来反序列化json:

public class DataObject
    {
        public ColDetails[][] MyData { get; set; }
    }

    public class ColDetails
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public Col4[] Col4 { get; set; }
        public string Col5 { get; set; }
    }

    public class Col4
    {
        public Value[] Value { get; set; }
    }

    public class Value
    {
    }
然后,在转换到类后返回所需的对象

使用以下代码进行反序列化:

JsonConvert.DeserializeObject<DataObject>(jsonString);

你能添加你用来反序列化的类和api响应吗?@ManpritSinghSahota你好,我添加了api响应,我用来反序列化的是这个IEnumerables有时你在json中的Col4是一个列表,有时是一个字符串。Col4不能像这样。@yiyiyiyou对不起,我的错,Col4总是一个列表,有时由API返回,有时与上面的示例不同,您可以添加用于反序列化的类以及API响应吗?@ManpritSinghSahota您好,我添加了API响应,我用于反序列化的是这个IEnumerablesomes json中的Col4是一个列表,有时它是一个字符串。Col4不能是这样的。@YiyiYou对不起,我的错,Col4总是一个列表,有时是由API返回的,有时不是上面的示例执行此操作无法将System.Generics.List隐式转换为System.Generics.IEnumerable执行此操作以避免返回错误IEnumerableJsonConvert.DeserializeObjectjsonString;但现在我得到了这个错误:InvalidCastException:无法将类型为“System.Collections.Generic.List1[System.Collections.Generic.List1[ProjectName.Models.Cols]]”的对象强制转换为类型为“System.Collections.Generic.IEnumerable`1[ProjectName.Models.Cols]'我已更新了答案。您需要返回任务执行此操作无法隐式将System.Generics.List转换为System.Generics.IEnumerable执行此操作以避免返回错误IEnumerableJsonConvert.DeserializeObjectjsonString;但现在我得到了这个错误:InvalidCastException:无法将类型为“System.Collections.Generic.List1[System.Collections.Generic.List1[ProjectName.Models.Cols]]”的对象强制转换为类型为“System.Collections.Generic.IEnumerable`1[ProjectName.Models.Cols]”我已经更新了答案。您需要返回任务
html += "<tr><td>" + json[x].col1+ "</td><td>" + json[x].col2+ "</td></tr>";
public class DataObject
    {
        public ColDetails[][] MyData { get; set; }
    }

    public class ColDetails
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public Col4[] Col4 { get; set; }
        public string Col5 { get; set; }
    }

    public class Col4
    {
        public Value[] Value { get; set; }
    }

    public class Value
    {
    }
JsonConvert.DeserializeObject<DataObject>(jsonString);