0

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); 
7
  • 1
    Why TestSubClass is an anonymous, if you know its structure? Is it always a 3 tokens inside it? Does it make sense to declare it as an array? Commented Mar 23, 2020 at 20:15
  • Yes,it's an array but it doesn't have a name, so I don't know how to deserialize it Commented Mar 23, 2020 at 20:17
  • Can you talk to author of the JSON to see if it can be set to makes a bit more sense (array of strings and numbers is very strange, especially since you trying to read those strings as float). Commented Mar 23, 2020 at 20:34
  • I agree that it's really strange, but no I can't ask to change the JSON since this being extracted from a piece of hardware. Commented Mar 23, 2020 at 20:39
  • How is it being "extracted" from a "piece of hardware"? What does that mean? Who is extracting it? How is being extracted? Commented Mar 23, 2020 at 20:42

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

is that Dictionary<Array<object>> exist?
I get your point Rob, but actually the whole json is an array, I just didn't know that you could put different types in a array. So I will continue on this approach. Thank you!
Sajid: That type (actually combination of types) is available via the System.Collections.Generic namespace. It is a Dictionary<object>, and the object is of type Array<object>.
@RobJarvis the Dictionary<TKey, TValue> must have two type arguments - for keys and for values
@Impworks you are right--my apologies for the oversight.
0

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) { // ... } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.