Newtonsoft JSON Deserialize Issue [Error converting value to type]

42,164

Solution 1

The Deserialization output for the JSON is when trying with

JSONArray Accounts = JsonConvert.DeserializeObject<JSONArray>(json, new JsonSerializerSettings
                           {
                               NullValueHandling = NullValueHandling.Ignore
                           });

is

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

But if you try like this

List<AccountInfo> lc = JsonConvert.DeserializeObject<List<AccountInfo>>(json, new JsonSerializerSettings
{
    NullValueHandling = NullValueHandling.Ignore
});

or

List<AccountInfo> lc = JsonConvert.DeserializeObject<List<AccountInfo>>(json);

will give you the resultant json into Object.

Attaching Screenshot

Solution 2

Your JSOn is not an object, but an array of objects, so you don't need a class to wrap the array, you should deserialize directly to array:

var Accounts = JsonConvert.DeserializeObject<List<AccountInfo>>(responseBody, 
               new JsonSerializerSettings
                {
                   NullValueHandling = NullValueHandling.Ignore
                });

If you really want to have JSONArray object, you could create it and serialize to it's property. Just to mention: your AccountInfo property is private, you should change it to public to deserialize to it.

JSONArray Accounts = new JSONArray
{
    AccountsInfo = JsonConvert.DeserializeObject<List<AccountInfo>>(responseBody,
           new JsonSerializerSettings
           {
             NullValueHandling = NullValueHandling.Ignore
           })
};
Share:
42,164
Mike
Author by

Mike

Updated on November 16, 2021

Comments

  • Mike
    Mike over 2 years

    Utilizing C# Newtownsoft JSON libraries... I have run into this issue.

    To set the stage...

    I have this JSON from a RESTful Web Service:

    [
        {
            "CorporateArea": "Brampton",
            "ServiceAddress": "321 Heart Lake Road",
            "VendorName": "Enbridge Gas Distribution Inc",
            "MeterNumber": "502105",
            "RateClass": "NG-R6",
            "Department": "22603",
            "Account": "12008",
            "VendorID": "0000001195",
            "MeterLevelID": 2882,
            "SiteAddressID": 468,
            "MappingLocation": "Beckett Sproule",
            "ElectricalBilling": "",
            "EnergyLine": "",
            "CorporateGroup": "Public Works"
        }
    ]
    

    I also have these C# classes:

    public class AccountInfo
    {
        [JsonProperty("Account")]
        public string Account { get; set; }
    
        [JsonProperty("CorporateArea")]
        public string CorporateArea { get; set; }
    
        [JsonProperty("CorporateGroup")]
        public string CorporateGroup { get; set; }
    
        [JsonProperty("Department")]
        public string Department { get; set; }
    
        [JsonProperty("ElectricalBilling")]
        public string ElectricalBilling { get; set; }
    
        [JsonProperty("EnergyLine")]
        public string EnergyLine { get; set; }
    
        [JsonProperty("MappingLocation")]
        public string MappingLocation { get; set; }
    
        [JsonProperty("MeterLevelID")]
        public string MeterLevelID { get; set; }
    
        [JsonProperty("MeterNumber")]
        public string MeterNumber { get; set; }
    
        [JsonProperty("RateClass")]
        public string RateClass { get; set; }
    
        [JsonProperty("ServiceAddress")]
        public string ServiceAddress { get; set; }
    
        [JsonProperty("SiteAddressID")]
        public string SiteAddressID { get; set; }
    
        [JsonProperty("VendorID")]
        public string VendorID { get; set; }
    
        [JsonProperty("VendorName")]
        public string VendorName { get; set; }
    }
    
    public class JSONArray {
       public IList<AccountInfo> AccountsInfo { get; set; }
    }
    

    From these, I call this Newtownsoft Method:

    JSONArray Accounts = JsonConvert.DeserializeObject<JSONArray> (responseBody,
       new JsonSerializerSettings
       {
          NullValueHandling = NullValueHandling.Ignore
       });
    

    But everytime I do so, I get the exception Newtonsoft.Json.JsonSerializationException with the error message:

    Error converting value "[{"CorporateArea":"Brampton","ServiceAddress":"321 Heart Lake Road","VendorName":"Enbridge Gas Distribution Inc","MeterNumber":"502105","RateClass":"NG-R6","Department":"22603","Account":"12008","VendorID":"0000001195","MeterLevelID":2882,"SiteAddressID":468,"MappingLocation":"Beckett Sproule","ElectricalBilling":"","EnergyLine":"","CorporateGroup":"Public Works"}]" to type 'TestWebService_Consume.JSONArray'. Path '', line 1, position 421.

    I've tried messing with the JSON string so it's not an array, and casting it into a simple AccountsInfo object, it returns the same error.

    I must be doing something wrong, but it's been some time since I've worked with the Newtonsoft JSON libraries, so I'm at a loss of what could possible be the issue here.