0

I am trying to deserialize a json which I get from a public web service (I have no access on it). This json has a key let's call it "termene" and for this key the value is sometimes a list and sometimes just a string. and I get the following error in visual studio:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List Because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObject Attribute can also be added to the type to force it to deserialize from a JSON object.

I am using the following code:

List<DosareICCJ> result;
var url = "some url that works";

var httpRequest = (HttpWebRequest)WebRequest.Create(url);


var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
var streamReader = new StreamReader(httpResponse.GetResponseStream());

var callresult = streamReader.ReadToEnd();
result = JsonConvert.DeserializeObject<List<DosareICCJ>>(callresult);

return result;

the "termene" key is defined like this in the class "DosareICCJ"

public List<TermeneICCJ> termene { get; set; }

   public class DosareICCJ
{
    public string ora { get; set; }
    public string numar { get; set; }
    public string numarVechi { get; set; }
    public DateTime? data { get; set; }
    public DateTime? dataInitiala { get; set; }
    public string categorieCaz { get; set; }
    public string departament { get; set; }
    public string obiect { get; set; }
    public string obiecteSecundare { get; set; }
    public string stadiuProcesual { get; set; }
    public string stadiulProcesualCombinat { get; set; }
    public List<PartiICCJ> parti { get; set; }      
    public List<TermeneICCJ> termene { get; set; }
    public List<CaiAtacICCJ> caiAtac { get; set; }
}
public class TermeneICCJ
{
    public DateTime? data { get; set; }
    public string ora { get; set; }
    public string SedintaDeJudecata { get; set; }
    public string complet { get; set; }
    public string numarDocument { get; set; }
    public DateTime? dataDocument { get; set; }
    public string tipDocument { get; set; }
    public string solutie { get; set; }
    public string detaliiSolutie { get; set; }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
  • So it's either `List` or just `DosareICCJ`? Could try to use try-catch around the first approach and fall back to the other one. – hkarask Sep 14 '22 at 07:43
  • the code snippet should return a list, everything shoud be a list. I was thinking of the try-catch approach but I only get one big json with a lot of "DosareICCJ"s so everything needs to work in one go – patru marian Sep 14 '22 at 07:51
  • Can you share the different variations of JSON? – hkarask Sep 14 '22 at 08:32
  • yes, the two of them can be found here https://textsaver.flap.tv/lists/51gi sorry for the format try to search for "termene" – patru marian Sep 14 '22 at 08:42
  • @patrumarian you have to post TermeneICCJ class at least if you want some help – Serge Sep 14 '22 at 10:16
  • 1
    It is old thread but I think it answers your issue: https://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n – skraszki Sep 14 '22 at 12:44
  • One of the converters from [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/q/18994685/3744182) should meet your needs. Deserialize to `List` and add `SingleOrArrayConverter` to your converters list. – dbc Sep 14 '22 at 15:55

0 Answers0