MVC/WebApi:类型“Newtonsoft.Json.JsonSerializationException”的异常-从发布请求检索数组时

8yparm6h  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(165)

我正在学习如何在POST中发送两个参数,这两个参数作为有效负载分组在一个对象数组中。
Angular JS:

var parData = { 'panelists': JSON.stringify($scope.arr), 'id': $scope.webId };
                
$http.post("/api/addPanelists", parData)
    .then(function (data, status, headers, config) {
}), function (data, status, headers, config) {
    alert("An error occurred during the request");
};

服务器端:
班级小组成员:

public class Panelists
{
    public string name { get; set; }
    public string email { get; set; }
}

添加Maven组成员Api控制器:

[HttpPost]
    public void CreatePanelists(Newtonsoft.Json.Linq.JObject data)
    {
       
        List<Panelists_DataImport.Panelist> panelistList = new List<Panelists_DI.Panelist>();
        panelistList = data["panelist"].ToObject<List<Panelists_DataImport.Panelist>>();
        webID = data["webId"].ToObject<Panelists_DataImport.Webinar>();

    }

数据内容:

{{  "panelists": "[{\"name\":\"Jack Anderson\",\"email\":\"janderson@email.com\"},{\"name\":\"Ed Johnson\",\"email\":\"ejohnson@email.com\"},{\"name\":\"Dead Poole\",\"email\":\"dpoole@email.com\"},{\"name\":\"Hank  Schmidt\",\"email\":\"hschmidt@email.com\"},{\"name\":\"Steven Alves\",\"email\":\"salves@email.com\"}]",  "id": "94395753143"}}

当我到达终点线

panelistList = data["panelists"].ToObject<List<Panelists>>();

我收到此错误:

Error converting value "[{"name":"Jack Anderson","email":"janderson@email.com"},{"name":"Ed Johnson","email":"ejohnson@email.com"},{"name":"Dead Poole","email":"dpoole@email.com"},{"name":"Hank  Schmidt","email":"hschmidt@email.com"},{"name":"Steven Alves","email":"salves@email.com"}]" to type 'System.Collections.Generic.List`1[Panelists_DataImport.Panelists]'. Path ''.

我怎样才能正确地从post请求中检索数组?我使用的类型不正确吗?
我现在被困住了,任何帮助都将不胜感激。
谢谢你,伊拉斯莫

    • 更新代码***(不工作,尚未理解如何执行条款)*
[HttpPost]
    public void CreatePanelists(Newtonsoft.Json.Linq.JObject data)
    {

        string webID = data["id"].ToString();

        IList<Panelist> panelistList = new IList<Panelist>;
    }

    public class Panelist
    {
        public string name { get; set; }
        public string email { get; set; }
    }

    public class Parameters
    {
        public IList<Panelist> panelists { get; set; }
        public string id { get; set; }
    }

更新新代码和错误窗口的屏幕截图:

juzqafwq

juzqafwq1#

你做错事了。
有效的JSON为

{
    "panelists": [{
        "name": "Jack Anderson",
        "email": "janderson@email.com"
    }, {
        "name": "Ed Johnson",
        "email": "ejohnson@email.com"
    }, {
        "name": "Dead Poole",
        "email": "dpoole@email.com"
    }, {
        "name": "Hank  Schmidt",
        "email": "hschmidt@email.com"
    }, {
        "name": "Steven Alves",
        "email": "salves@email.com"
    }],
    "id": "94395753143"
}

课堂应该是这样的。

public class Panelist
{
    public string name { get; set; }
    public string email { get; set; }
}

public class Example
{
    public IList<Panelist> panelists { get; set; }
    public string id { get; set; }
}

你应该像这样访问它。
与此不同
更新:

[HttpPost]
public void CreatePanelists(string data)
{

    Parameters parameters = JsonConvert.DeserializeObject<Parameters>(data);

    IList<Panelist> panelistList = parameters.panelists;
}

相关问题