Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
An exception of type 'System.InvalidCastException' occurred in RetrieveData.dll but was not handled in user code

Additional information: Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'RetrieveData.Models.FlightDetails'.

What I have tried:

try to Convert json Data to Class Object Flight Details above i Post the Error Please help-


XmlDocument doc = new XmlDocument();
doc.LoadXml(dep);
string jsonText = JsonConvert.SerializeXmlNode(doc);

// DataTable tester = (DataTable)JsonConvert.DeserializeObject(jsonText, (typeof(DataTable)));
var table = JsonConvert.DeserializeAnonymousType(jsonText, new { Makes = default(DataTable) }).Makes;
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
FlightDetails routes_list = (FlightDetails)json_serializer.DeserializeObject(jsonText);
Posted
Updated 30-Nov-18 15:26pm
Comments
F-ES Sitecore 30-Nov-18 7:50am    
Without knowing what you are trying to convert, and what you are trying to convert it to it's impossible to give specific advice. Google "c# deserialise json" for things that might help.

Start by looking at what the json contains. This may help: json2csharp - generate c# classes from json[^] - it generates C# clases from a JSON string that you can then use (or compare with) the classes in your app.

Me? I use Newtonsoft, where creating the instances and filling them is simple:
C#
MyClass mc = Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(jsonString);
where MyClass can be anything: List<T>, a single class instance, whatever you JSON contains.
 
Share this answer
 
I have an article that I put together based on questions here. It covers all that you need: Working with JSON in C# & VB[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900