【问题标题】:changing the json deserialized type to an array将 json 反序列化类型更改为数组
【发布时间】:2020-04-22 13:08:42
【问题描述】:

我已经用实体框架搭建了一个 web api 控制器。 每当我发布一个 json 对象时,它都会起作用。但是每当 JSON 对象包含在数组 [{},{},] 中时,我都会不断收到错误消息。 下面通过 Postman 报错:

"Message": "The request is invalid.",
"ModelState": {
    "incidence": [
        "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'TSCAPP.Models.Incidence' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '', line 1, position 1."

POST 控制器:

    [ResponseType(typeof(Incidence))]
    public IHttpActionResult PostIncidence(Incidence incidence)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Incidences.Add(incidence);
        db.SaveChanges();
        return CreatedAtRoute("DefaultApi", new { id = incidence.RegistrationID }, incidence);
    }

发病率模型

public class Incidence
{
    [Key]
    public int RegistrationID { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    [MinLength(6)]
    public string TSCNO { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    public string EmailAddress { get; set; }

    public string CellPhone { get; set; }
    public string IncidenceType { get; set; }
    public string RequestType { get; set; }
    public string County { get; set; }
    public string ExactLocation { get; set; }
    public string Request { get; set; }
    public int? RoleID { get; set; }
    [DataType(DataType.MultilineText)]
    public string FeedBack { get; set; }
}

已发布 JSON 文件

[ { “注册ID”:1, “姓名”:“埃文”, “TSCNO”:“662621”, "电子邮件地址": "evan@mail.com", “手机”:“254722543778”, “发病类型”:空, “请求类型”:空, “县”:“西波科特”, "ExactLocation": "Kapenguria", “请求”:“无”, “角色ID”:2, “回馈”: ”” }, { “注册ID”:2, “姓名”:“约翰”, “TSCNO”:“607921”, "电子邮件地址": "john@mail.com", “手机”:“254700172168”, “发病类型”:空, “请求类型”:空, “县”:“夸莱”, "ExactLocation": "Mkongani", “请求”:“依赖”, “角色ID”:2, “回馈”: ”” } ]

【问题讨论】:

  • 向我们展示您发布的 Incidence 类和 json

标签: c# json asp.net-mvc


【解决方案1】:

如果您期望一个或多个元素,模型绑定无法神奇地理解。格式不兼容。如果要绑定数组,则必须定义另一个端点。

public IHttpActionResult PostIncidences(Incidence[] incidences)

【讨论】:

  • 请进一步详细说明。
  • 在 JSON 中,单个对象 ({}) 和对象数组 ([{},{}]) 使用不同的语法定义。您必须告诉 ASP.NET 您期望的是哪一个,并且它们不能互换使用。因此,您必须为每个方法设置单独的操作方法:一个需要单个对象,另一个需要一个数组。
【解决方案2】:

@ufuk-hacıoğulları 我已将其更改为此并按预期工作

    [ResponseType(typeof(Incidence))]
    public IHttpActionResult PostIncidence([FromBody] List<Incidence> incidence)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        foreach (var orp in incidence)
        {
            db.Incidences.Add(orp);
            db.SaveChanges();
        }
        return Ok(incidence);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    相关资源
    最近更新 更多