Search code examples
c#asp.net-mvcdeserializationjson-deserialization

Cannot convert json to object


Hello i've got this error Message

"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ClassLibraryMifosX.ViewModels.Rootobject2' 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 JS

my deserialize code :

Rootobject Rsc = JsonConvert.DeserializeObject<Rootobject>(json);

my class with json object description:

public class Rootobject
{
    public List<Class1> Property1 { get; set; }
}

public class Class1
{
    public int entityId { get; set; }
    public string entityAccountNo { get; set; }
    public string entityExternalId { get; set; }
    public string entityName { get; set; }
    public string entityType { get; set; }
    public int parentId { get; set; }
    public string parentName { get; set; }
    public string entityMobileNo { get; set; }
    public Entitystatus entityStatus { get; set; }
}

public class Entitystatus
{
    public int id { get; set; }
    public string code { get; set; }
    public string value { get; set; }
}

my json :

[
  {
    "entityId": 1,
    "entityAccountNo": "000000001",
    "entityExternalId": "100001-241563",
    "entityName": "Smith W R",
    "entityType": "CLIENT",
    "parentId": 1,
    "parentName": "Head Office",
    "entityMobileNo": "254728000000",
    "entityStatus": {
      "id": 300,
      "code": "clientStatusType.active",
      "value": "Active"
    }
  },
  {
    "entityId": 310,
    "entityAccountNo": "000000310",
    "entityName": "John Smith",
    "entityType": "CLIENT",
    "parentId": 14,
    "parentName": "TestOffice1",
    "entityStatus": {
      "id": 300,
      "code": "clientStatusType.active",
      "value": "Active"
    }
  },
  {
    "entityId": 422,
    "entityAccountNo": "000000422",
    "entityExternalId": "smith1",
    "entityName": "Smith Jones",
    "entityType": "CLIENT",
    "parentId": 11,
    "parentName": "Barquisimeto",
    "entityMobileNo": "88989898",
    "entityStatus": {
      "id": 300,
      "code": "clientStatusType.active",
      "value": "Active"
    }
  },
  {
    "entityId": 774,
    "entityAccountNo": "000000774",
    "entityName": "John AAA Smith",
    "entityType": "CLIENT",
    "parentId": 1,
    "parentName": "Head Office",
    "entityStatus": {
      "id": 300,
      "code": "clientStatusType.active",
      "value": "Active"
    }
  },
  {
    "entityId": 1789,
    "entityAccountNo": "Head Office000001789",
    "entityExternalId": "547222",
    "entityName": "Kaitlin Smith",
    "entityType": "CLIENT",
    "parentId": 1,
    "parentName": "Head Office",
    "entityStatus": {
      "id": 300,
      "code": "clientStatusType.active",
      "value": "Active"
    }
  }
]

what i have been done wrongly ? Thanks


Solution

  • There is no root object into your Json data so just deserialize it as a collection of Class1 like below:

    var collection = JsonConvert.DeserializeObject<List<Class1>>(json);
    

    Don't forget that VS can create for you a class that can be used to deserailize your Json data. You don't need to write yourself the definition of Class1. Just go to menu => Edit > Paste Special > Paste JSON as classes