0

I am trying to get the values of this object, this object was a dynamic property in a parsed json object. When I try getting the information i keep getting returned 12 properties. One is JToken.First, JToken.Last. These give the information in the right way, but they do not give all of the information, the goal is to get all of the information stored in the dynamic object.

public class JsonWrapper
{
    public static dynamic UnWrapDynamic(dynamic obj)
    {
        PropertyInfo[] objectInfo = obj.GetType().GetProperties();

        foreach (PropertyInfo propertyInfo in objectInfo)
        {
            if (propertyInfo.PropertyType.Name == nameof(JToken) && propertyInfo.Name == nameof(JToken.First))
            {
                return (JToken)propertyInfo.GetValue(obj).Values;
            }
        }

        return null;
    }
}
  1. List item
quaabaam
  • 1,808
  • 1
  • 7
  • 15
Vexea
  • 1
  • 3
  • Defeats the purpose of dynamic... You are better off parsing to json objects instead – Jonathan Alfaro May 04 '21 at 19:53
  • 1
    [`JToken`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm) is a generic "thing" that has been parsed from JSON by Json.NET. `First` and `Last` are shortcuts to the first and last children of the token. If you can show how you're getting the `dynamic` object, I'm guessing you could be supplied with many ways of iterating over its properties. – Heretic Monkey May 04 '21 at 19:54
  • 1
    Like, say, [How do I enumerate through a JObject?](https://stackoverflow.com/q/10543512/215552) – Heretic Monkey May 04 '21 at 19:55
  • Alright, thank you i will look into it later, i have to sleep now. – Vexea May 04 '21 at 20:14

0 Answers0