1

I want to deserialize a JSON string that I don't know its Type to an object of type Object and be able to access its properties using reflection. when I used this

object myObject = JsonConvert.DeserializeObject("{\'Id\':\'1\'}");

the type of myObject is of type JObjectand I am not able to access its properties using reflection.

is there a way to do so using Json.net or any other JSON deserializer?

Heba Gomaah
  • 1,145
  • 4
  • 11
  • 16
  • 1
    This seems like a duplicate of this: http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – scotru Dec 11 '16 at 08:31
  • 1
    Or this with JSON.NET: http://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net – scotru Dec 11 '16 at 08:33
  • @scotru I've tried the dynamic too. My point is that I need to access its properties using reflection. the generated dynamic object is also of type JObject – Heba Gomaah Dec 11 '16 at 08:40
  • Your question should be answered under this link: [JSON deserialize](http://stackoverflow.com/questions/35238194/deserialize-json-with-json-net-into-c-sharp-dynamic) – Moerwald Dec 11 '16 at 08:40
  • @AndreasM. I've tried the dynamic too. My point is that I need to access its properties using reflection. the generated dynamic object is also of type JObject – Heba Gomaah Dec 11 '16 at 08:57
  • Hmm, still not following the difficulty here--is the question how to use reflection on dynamic objects? If so see http://stackoverflow.com/questions/8631546/get-property-value-from-c-sharp-dynamic-object-by-string-reflection – scotru Dec 11 '16 at 08:57
  • @HebaGomaah have you tried to create an object that contains all properties – BRAHIM Kamel Dec 11 '16 at 08:59
  • @scotru the question is, is there any deserializer that can convert a json string to an object just as if the type of the data is a predefined type? not to a JObject – Heba Gomaah Dec 11 '16 at 09:25
  • @HebaGomaah I understand now. Is the Json simple (attribute value pairs?) If so I think grrrrrs answer is best. If not maybe you could use an ExpandoObject as described here: http://stackoverflow.com/questions/22906010/deserialize-dynamic-json-string-using-newtonsoft-json-net AND https://stackoverflow.com/questions/2594527/how-do-i-iterate-over-the-properties-of-an-anonymous-object-in-c – scotru Dec 11 '16 at 09:35

2 Answers2

2

I think you can deserialize the object into either a Dictionary<string,string> or an expandoobject (also castable to IDictionary<string,object>) and then you don't need to use reflection to get at the properties, you can get them through the dictionary.

See: Deserialize Dynamic Json string using Newtonsoft JSON.NET

Community
  • 1
  • 1
gmn
  • 4,199
  • 4
  • 24
  • 46
1

This doesn't let you use reflection per-se but an ExpandoObject does let you iterate over the properties:

        string json = "{\'Id\':\'1\'}";
        var converter = new ExpandoObjectConverter();
        dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);

        IDictionary<string, object> dict = (IDictionary<string, object>)obj;
        foreach (string key in dict.Keys)
        {
            Console.WriteLine(key);
        }
scotru
  • 2,563
  • 20
  • 37