JSONPatch WCF REST API 获取无效负载错误

Posted

技术标签:

【中文标题】JSONPatch WCF REST API 获取无效负载错误【英文标题】:JSONPatch WCF REST API getting invalid payload error 【发布时间】:2018-03-14 17:32:48 【问题描述】:

我正在尝试在我的 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."

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

【问题讨论】:

【参考方案1】:

我可以看到您的 JSON 对象无效

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

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

【讨论】:

以上是关于JSONPatch WCF REST API 获取无效负载错误的主要内容,如果未能解决你的问题,请参考以下文章

WCF Web API WCF REST 和 Web Service 的区别

ASP.Net Web API 与 WCF - Web API 能否用于向单例 WCF 服务提供基于 REST 的通信?

如何在 WCF 服务中调用 REST API?

web service, wcf, wcf rest, web api之间的区别

Web Service vs WCF vs WCF REST vs Web API

Web Service vs WCF vs WCF REST vs Web API