1

This may be too much trouble, but I have an object I'm trying to deserialize that looks like this:

{
"Status": { ... a status object ... }
"Data" : {... could be any one of 10 other data classes I have ... }
}

I want to deserialize this object into a response class that looks like this:

public class Response
{
        public Status Status;

        public Stream Data;
}

Then later deserialize the Data attribute into another class. However, if I make the type of the data attribute a Stream, I get a Security Exception when I try to deserialize. Likewise, if I make it a string, the deserialize just calls the toString() method on the data object (after deserializing it) and puts that as the attribute, which isn't helpful.

Is there any way to accomplish something like this? I'm trying to avoid having 10 different classes that all look a lot like Response but just have a different type for the Data attribute. If there's a way that I can dynamically set the type of the Data attribute that might also solve the situation.

Thanks for any help!

Nico
  • 1,181
  • 2
  • 17
  • 35
  • 1
    Not a direct answer but It may be good to see other ways http://stackoverflow.com/questions/9522044/trying-to-consume-smartystreets-json-with-json-net-cannot-deserialize-json-a/9522266#9522266 – L.B Mar 13 '12 at 08:02
  • Hmmmmm that's pretty cool. If I try to access an attribute of my dynamic jsonObj that's actually still an object, will I get back the json string for that object? – Nico Mar 13 '12 at 08:13
  • "attribute of my dynamic jsonObj"? sorry I couldn't understand – L.B Mar 13 '12 at 08:16
  • Sorry for not being clearer, it's the nature of this kind of thing. Let's use the example you posted there: if I call dynamic jsonObj = JsonUtils.JsonObject.GetDynamicJsonObject(json); on it and then try to access item.components (for one of the items in that array) will I get the components object still in json? Thanks for you help so far btw – Nico Mar 13 '12 at 08:25
  • 1
    Yes every field/property in dynamic object is another dynamic object(if it is not a base type like int,string etc.) – L.B Mar 13 '12 at 08:54

1 Answers1

-1

I always use Newtonsoft.Json.dll to solve this problem.

EasonBlack
  • 4,196
  • 4
  • 21
  • 24