2017-10-03 11 views
0

J'essaie d'utiliser JSONPatch (version KevinDockx) dans mon API WCF REST (ASP.NET v4.5). Mon contrat d'exploitation se présente comme suit: -API JSONPatch WCF REST obtenant une erreur de charge utile invalide

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

et la mise en œuvre comme suit: -

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

Mes données est comme le format ci-dessous où je veux effectuer ajouter, mettre à jour supprimer, déplacer et les opérations de swap sur le tableau des participants.

{ 
    "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" 
      } 
     } 
    ] 
} 

Sur JSON désérialisation Je reçois ci-dessous erreur: -

{ 
    "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." 
} 

Pouvez-vous me aider avec ce que je suis absent dans ce domaine? Y a-t-il quelque chose de supplémentaire à faire?

Répondre

0

Comme je peux voir votre objet JSON est pas valide

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

Je vérifiais juste pour toute autre erreur dans votre objet JSON et passer un JSON valide corrigera l'erreur.