7

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.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
Mike
  • 874
  • 1
  • 8
  • 15
  • 1
    Possible duplicate of [Deserializing JSON Object Array with Json.net](http://stackoverflow.com/questions/18192357/deserializing-json-object-array-with-json-net) – Maksim Simkin Dec 21 '16 at 22:07

3 Answers3

7

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

Mohit S
  • 13,723
  • 6
  • 34
  • 69
3

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
           })
};
Tyler
  • 25
  • 9
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
0

I use Newtonsoft.Json in generic read and write methods for Excel spreadsheets. Had this exception start throwing, and turned out to be a change made to the excel spreadsheet. In general column types, the first cell is used to set the data table type, generally with a header column this ends up being a string. The excel I was trying to load had a new row added at the top for zero indexing, so all columns were being set as system.double.

Just thought I'd throw that here, since it took a while to track down why this error started happening after years of working perfectly.

Dan
  • 51
  • 5