【问题标题】:Patching an object with an array of objects用对象数组修补对象
【发布时间】:2018-08-31 03:59:11
【问题描述】:

我在学校的课程和学生之间存在多对多关系。这是我的课程:

public class Course
{
    public Guid CourseId { get; set; }
    public string Name { get; set; }

    public ICollection<CourseStudent> Students { get; set; }
}

public class CourseStudent
{
    public Guid CourseId { get; set; }        
    public Guid StudentId { get; set; }
}

我正在使用 JsonPatch 修补我的对象。我正在尝试添加到Students 的集合中,就在集合的末尾:

[
  {
    "op": "add",
    "path": "/Students/-",
    "value": [
       { 
         "CourseId": "07264DC9-9FEB-42E2-B1EF-08D58F58C873", 
         "StudentId": "FB6E6988-4A56-4CA4-86E2-E23090FAD98F"
       }
     ]
  }
]

但是当我提交这个时,我得到一个异常说:

“类名”:“Microsoft.AspNetCore.JsonPatch.Exceptions.JsonPatchException”,

"Message": "值 '[\r\n {\r\n \"CourseId\": \"07264DC9-9FEB-42E2-B1EF-08D58F58C873\",\r\n \"StudentId\" : \"FB6E6988-4A56-4CA4-86E2-E23090FAD98F\"\r\n }\r\n]' 对目标位置无效。",

根据 Json Patch 文档,我的结构看起来是正确的。知道为什么它不接受我的格式吗?

【问题讨论】:

    标签: c# json asp.net-core .net-core json-patch


    【解决方案1】:

    终于弄明白了,格式应该是:

    [
      {
        "op": "add",
        "path": "/Students/-",
        "value":
           { 
             "CourseId": "07264DC9-9FEB-42E2-B1EF-08D58F58C873", 
             "StudentId": "FB6E6988-4A56-4CA4-86E2-E23090FAD98F"
           }
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      相关资源
      最近更新 更多