Cainnech
Cainnech

Reputation: 459

Deserializing an anonymous object

I'm currently trying to deserialize a Json-feed that I'm receiving from an external process. The problem is that the JSON is relying heavily on anonymous classes which I don't know how to properly deserialize.

I'm hoping anyone here could help me.

Below is a piece of the JSON:

[
  14,
  {
    "a": [
      "5877.40000",
      0,
      "0.89672653"
    ],
    "b": [
      "5877.30000",
      6,
      "6.20000000"
    ],
    "c": [
      "5877.40000",
      "0.02216247"
    ]
},
"name", 
"description"
]

So my class currently looks like this, but this is not correct:

    public class TestClass
    {
        public int ChannelID { get; set; } // 14 in the sample

        public TestSubClass test { get; set; } // THIS IS THE ANONYMOUS ONE

        public string ChannelName { get; set; } // "name" in the sample

        public string ChannelDescription { get; set; } // "description in the sample"
    }


    public class TestSubClass 
    {
        public TestOption1Class a { get; set; }
        public TestOption1Class b { get; set; }
        public TestOption2Class c { get; set; }
    }

    public class TestOption1Class 
    {
        public float price { get; set; }
        public int amount { get; set; }
        public float unitWeight { get; set; }
    }

    public class TestOption2Class 
    {
        public float price { get; set; }
        public float unitWeight { get; set; }
    }

Now these classes are wrong. I am assuming I need to use some sort of key/value pair or something, but I'm not sure on how to achieve that. If somebody know how I could deserialize this properly, that would be great.

In order to be complete, the code to deserialize is below:

var test = JsonConvert.DeserializeObject<List<TestClass>>(jsonTest);

Upvotes: 0

Views: 124

Answers (2)

RobJarvis
RobJarvis

Reputation: 370

Since the anonymous type you're struggling with has keys and values, I think I would do away with the TestSubClass class altogether and try this instead:

public class TestClass
    {
        public int ChannelID { get; set; } // 14 in the sample

        public Dictionary<string, Array<object>> test { get; set; } // THIS IS THE ANONYMOUS ONE

        public string ChannelName { get; set; } // "name" in the sample

        public string ChannelDescription { get; set; } // "description in the sample"
    }

Deserialized, you would wind up with a Dictionary of Arrays containing Objects that you can then operate on with Double.TryParse() or Int32.TryParse() to get the proper values.

Upvotes: 1

Impworks
Impworks

Reputation: 2818

You can use a combination of strongly-typed classes for parts of the response structure you know and JToken for parts that can change and need to be inspected at runtime. Something like this:

public class TestClass
{
    public int ChannelID { get; set; } // 14 in the sample

    [JsonProperty("test")]
    public JToken Test { get; set; } // THIS IS THE ANONYMOUS ONE

    public string ChannelName { get; set; } // "name" in the sample
    public string ChannelDescription { get; set; } // "description in the sample"
}

Then you can use it like this:

var data = JsonConvert.DeserializeObject<TestClass>("...");
var test = data.Test;
if(test.Type == JTokenType.Array)
{
    // process as array
    foreach (JToken elem in test)
    {
        // ...
    }
}
else if(test.Type == JTokenType.Object)
{
    // process as object
    foreach (JProperty kvp in test)
    {
        // ...
    }
}

Upvotes: 0

Related Questions