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; }
}