C# .NET - Deserialization error - could not cast

Asked By Ravi Danum on 02-Apr-10 12:11 PM
Hello,

When deserializing a JSON object, I get the following error:

Unable to cast object of type 'System.Collections.Generic.Dictionary'2[System.String,System.Object]' to Query1Request.

The chain of events surrounding this error are:

1) Read JSON  from the Http request.
2) Deserialize it to a JSON object.
3) Serialize it to a string and store into a serializable object as a string.
4) Serialize the object to a file (binary serialization).
5) Deserialize the file contents into an object.
6) read the string (JSON serialized object).
7) Finally, deserialize the string into a JSON object.

Step 7 is where I get the error.  It doesn't like the cast from object to the JSON object, Query1Request.

Any ideas why JSON could be deserialized into Query1Request when it was first read from the request, but not later after the steps above?

Thanks for any help.

-ravi


Solved - re: Deserialization error - could not cast

Ravi Danum replied to Ravi Danum on 02-Apr-10 12:50 PM
Hello,

I changed: DeserializeObject(<string>)
to
Deserialize<Query1Request>(<string>)

-ravi