0

我正在尝试在我的 WCF REST API(ASP.NET v4.5)中使用 JSONPatch(KevinDockx 版本)。我的经营合同如下:-

[OperationContract]
[WebInvoke(UriTemplate = "/{raceId}/participants", Method = "PATCH")]
void UpdateRace(string id, JsonPatchDocument<ParticipantContract[]> participantsContract);

并实现如下: -

public void UpdateRace(string id, JsonPatchDocument<ParticipantContract[]> participantsContract)
{
    //Someoperation
}

我的数据类似于下面的格式,我想在参与者数组上执行添加、更新、删除、移动和交换操作。

{
    "raceId" : 1
    "participants": [
        {
            "id": "abc",
            "car": "Mercedes",
            "model": "F1 W08 EQ Power",
            "teamname": "Mercedes-AMG Petronas Motorsport",
            "driver": {
                "id": "111",
                "firstname": "Lewis",
                "lastname": "Hamilton",
                "age": "29"
            },
            "codriver": {
                "id": "222",
                "firstname": "Valtteri",
                "lastname": "Bottas",
                "age": "32"
            }
        },
        {
            "id": "def",
            "car": "Ferrari",
            "model": "SF70H",
            "teamname": "Scuderia Ferrari",
            "borrower": {
                "id": "333",
                "firstname": "Sebastian",
                "lastname": "Vettel",
                "age": "30"
            },
            "coborrower": {
                "id": "444",
                "firstname": "Kimi",
                "lastname": "Räikkönen",
                "age": "37"
            }
        }
    ]
}

在 JSON 反序列化时,我遇到以下错误:-

{
    "summary": "Bad Request",
    "details": "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ParticipantContract' 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."
}

你能帮我解决我在这方面所缺少的吗?有什么额外的事情需要做吗?

4

1 回答 1

0

正如我所见,您的 JSON 对象无效

    {
        "raceId" : 1  // you missed comma here
        "participants": [
         {

只需检查 JSON 对象中的任何其他错误并传递有效的 JSON 即可修复错误。

于 2017-12-06T11:19:14.007 回答