Newtonsoft JSON Deserialize

You can implement a class that holds the fields you have in your JSON class MyData { public string t; public bool a; public object[] data; public string[][] type; } and then use the generic version of DeserializeObject: MyData tmp = JsonConvert.DeserializeObject<MyData>(json); foreach (string typeStr in tmp.type[0]) { // Do something with typeStr } Documentation: … Read more

Deserializing polymorphic types with Jackson based on the presence of a unique property

Here’s a solution I’ve come up with that expands a bit on Erik Gillespie’s. It does exactly what you asked for and it worked for me. Using Jackson 2.9 @JsonDeserialize(using = CustomDeserializer.class) public abstract class BaseClass { private String commonProp; } // Important to override the base class’ usage of CustomDeserializer which produces an infinite … Read more

Deserializing JSON with dynamic keys

Seriously, no need to go down the dynamic route; use var deser = new JavaScriptSerializer() .Deserialize<Dictionary<string, Dictionary<string, int>>>(val); var justDaily = deser[“daily”]; to get a dictionary, and then you can e.g. foreach (string key in justDaily.Keys) Console.WriteLine(key + “: ” + justDaily[key]); to get the keys present and the corresponding values.

Deserialize XML To Object using Dynamic

You may want to try this. string xml = @”<Students> <Student ID=””100″”> <Name>Arul</Name> <Mark>90</Mark> </Student> <Student> <Name>Arul2</Name> <Mark>80</Mark> </Student> </Students>”; dynamic students = DynamicXml.Parse(xml); var id = students.Student[0].ID; var name1 = students.Student[1].Name; foreach(var std in students.Student) { Console.WriteLine(std.Mark); } public class DynamicXml : DynamicObject { XElement _root; private DynamicXml(XElement root) { _root = root; } … Read more

Polymorphism with gson

This is a bit late but I had to do exactly the same thing today. So, based on my research and when using gson-2.0 you really don’t want to use the registerTypeHierarchyAdapter method, but rather the more mundane registerTypeAdapter. And you certainly don’t need to do instanceofs or write adapters for the derived classes: just … Read more