Home > Blockchain >  Cannot deserialize current JSON array (e.g. [1,2,3]) to type because the type requires a JSON object
Cannot deserialize current JSON array (e.g. [1,2,3]) to type because the type requires a JSON object

Time:11-08

My NUnit assertion is failing with following error due to failing to deserialize the JSON array:

Newtonsoft.Json.JsonSerializationException : Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ‘xyzzy’ because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To 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 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. Path '', line 1, position 1.

Sample JSON output which I converted to C# from this site https://app.quicktype.io/:


[
    {
        "id": "008",
        “fid": "6556",
        “pids: [
            "1234",
            "5678"
        ]
    },
    {
        "id": "009",
        "fid": "6556",
        "pids": [
            "1234"
        ]
    }
]

Sample DTO where I used the converted JSON:

public class getUsers
{
  public string id { get; set; }
  public string fid { get; set; }
  public List<string> pids { get; set; }
}

And this is the test which is failing to deserialize:

[Test]
public void VerifyWorkspaceId()
{
  var api = new ServiceTests();
  var response = api.GetUsers();
  Assert.AreEqual("6556", response.fid[0]);
}

GetUsers where I am doing the deserialization:

public UserDto GetUsers()
{
  var client = new RestClient(“xyz.com”);
  var request = new RestRequest("/api/test”, Method.GET);

  request.AddHeader("Accept", "application/json");
  request.RequestFormat = DataFormat.Json;

  IRestResponse response = client.Execute(request);
  var content = response.Content;

  UserDto userdto = JsonConvert.DeserializeObject<UserDto>(content);
  return userdto;
}

CodePudding user response:

You tried to deserialise an array into a single object instead of a list / array. This is what the error message is telling you - it cannot logically convert a list into a single item.

In your code, UserDto userdto represents a single user, not a list of users (which is what the JSON array represents).

The error message also tells you how to solve it - e.g. by deserialising to a List<T>:

public List<UserDto> GetUsers()
{
  var client = new RestClient("xyz.com");
  var request = new RestRequest("/api/test", Method.GET);

  request.AddHeader("Accept", "application/json");
  request.RequestFormat = DataFormat.Json;

  IRestResponse response = client.Execute(request);
  var content = response.Content;

  List<UserDto> users = JsonConvert.DeserializeObject<List<UserDto>>(content);
  return users;
}

and

[Test]
public void VerifyWorkspaceId()
{
  var api = new ServiceTests();
  var response = api.GetUsers();
  Assert.AreEqual("6556", response[0].fid);
}
  • Related