Dragon
Dragon

Reputation: 2481

Cannot deserialize JSON object when using Json.NET

I send the following body with HTTP request serialized by Json.NET:

{"parties":[{"name":"Name","number":"Number"}]}

WebService checks whether there is a list of "parties" objects or just a single object. And depending on results of this check list of objects or single object is returned. And in situation with the body I provided I receive the following exception:

Test 'WebService.Tests.Tests.Post_create' failed: Newtonsoft.Json.JsonSerializationException: Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[JSON.WebService.Parties]'.

Is there any solution for such problem?

Updated:

Here is the Parties type:

public class WebRequestBody : JSONBody
{
    public List<Parties> parties { get; set; }
}

public class Parties : JSONBody
{
    public string name { get; set; }
    public string number { get; set; }
}

Web Service is waiting exactly for such a body :(

JSONBody is a class that describes serialization and the structure is too deep to copy it here. But if it is needed I can share it.

Upvotes: 0

Views: 562

Answers (1)

Marcelo Cantos
Marcelo Cantos

Reputation: 186068

It's difficult to tell without seeing the type of Parties and your JSON decoding code, but I'm guessing that you don't need the entire message you're sending. You might just need this:

[{"name":"Name","number":"Number"}]

Upvotes: 2

Related Questions