2

I am a newbie in Jil.JSON. I am using 'JSON.DeserializeDynamic' because the 'JSON' string always varies for different customer's requirements.

In my code, I am using 'foreach'. 'keyValue' is a dynamic with 'Key' and 'Value' properties.

I am trying to get the values of 'keyValue.Value Type' and 'keyValue.Value StringValue' but I do not know how to get these Non-Public members' values.

need to get the Type and StringValue

enter image description here

Here a sample of my code:

string json = "{\"Items\":[{ \"Name\":\"Ballpen\", \"Price\":12.3 },{ \"Name\":\"Pencil\", \"Price\":3.21 }],\"Date\":\"21112019\"}";

using (var str = new StringReader(json ))
   {
       foreach (var keyValue in JSON.DeserializeDynamic(@str, Jil.Options.IncludeInherited))
       {
           //check the keyValue.Value Type  
           //if keyValue.Value Type is string, get the StringValue     
       };
}

I tried using 'keyValue.GetType()' but it is giving me a different properties.

enter image description here

Edited:

I am almost there....

            json = "{\"Items\":[{ \"Name\":\"Ballpen\", \"Price\":12.3 },{ \"Name\":\"Pencil\", \"Price\":3.21 }],\"Date\":\"21112019\"}";
            Dictionary<string, object> la = JSON.Deserialize<Dictionary<string, object>>(json);

            foreach (var data in la)
            {
                Console.WriteLine("Key=" + data.Key);
                Console.WriteLine("Value=" + data.Value.ToString());

                Type t = data.Value.GetType();
                MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic |
                BindingFlags.Instance);
                foreach (MemberInfo member in members)
                {                    
                    if(member.Name == "Type")
                    {
                        Console.WriteLine(member.Name);
                        //member.MemberType.GetType();

                    }
                    if (member.Name == "StringValue")
                    {
                        Console.WriteLine(member.Name);                      
                    }
                }

            }


Key=Items
Value=[{
 "Name": "Ballpen",
 "Price": 12.3
}, {
 "Name": "Pencil",
 "Price": 3.21
}]
Type
StringValue
Key=Date
Value="21112019"
Type
StringValue

now, i need to get the 'member value'

any ideas?

grnmeado7
  • 31
  • 4
  • What's the problem? `Items` *is* an array. You'll have to iterate over its contents to get to individual items. Even with `dynamic` you still have to access the array members by index, eg have to write `dynamic someObject=JSON.DeserializeDynamic(..); var firstName=someObject.Items[0].Name;` or `foreach(dynamic item in someObject.Items){ var name=item.Name;}` – Panagiotis Kanavos Oct 15 '19 at 07:21
  • definitely you are correct ...my case here is how to get the 'non-public-members' value.... keyValue.Value.Type keyValue.Value StringValue however, these are not working – grnmeado7 Oct 15 '19 at 07:52
  • ![error] (https://ibb.co/7RGBPZw) i tried using keyValue.Value.Type i am getting error – grnmeado7 Oct 15 '19 at 08:48
  • If you want access to the raw values why not deserialize to `Dictionary` ? Or use JSON.NET - you aren't *deserializing* the JSON string, you're trying to access individual values. That's the job of the raw JSON objects provided by the two libraries – Panagiotis Kanavos Oct 15 '19 at 08:54
  • ![using Dictionary] (https://ibb.co/zXH9yZg) i tried using
    Dictionary
     but still... i cannot access 'Type' and 'StringValue' properties... am i doing something wrong?
    – grnmeado7 Oct 15 '19 at 10:03

1 Answers1

1

...not a good code but now, it is working.

...
           json = "{\"Items\":[{ \"Name\":\"Ballpen\", \"Price\":12.3 },{ \"Name\":\"Pencil\", \"Price\":3.21 }],\"Date\":\"21112019\"}";
            Dictionary<string, object> la = JSON.Deserialize<Dictionary<string, object>>(json);

            foreach (var data in la)
            {
                Console.WriteLine("Key=" + data.Key);
                Console.WriteLine("Value=" + data.Value.ToString());

                Type t = data.Value.GetType();
                MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic |
                BindingFlags.Instance);
                String sType = "";
                foreach (MemberInfo member in members)
                {
                    Object oValue;
                    if(member.Name == "Type")
                    {

                        oValue = GetValue(member, data.Value);
                        sType = oValue.ToString();
                        Console.WriteLine(member.Name + "=" + oValue);
                        //member.MemberType.GetType();                    

                    }
                    if (member.Name == "StringValue")
                    {   
                        if(sType == "String")
                        {
                            oValue = GetValue(member, data.Value);
                            Console.WriteLine(member.Name + "=" + oValue);
                        }

                    }
                }
                Console.WriteLine();
                Console.WriteLine();
            }
...

i need to downcast to FieldInfo or PropertyInfo to get the member's value

        private object GetValue(MemberInfo memberInfo, object obj)
        {
            switch (memberInfo)
            {
                case FieldInfo fieldInfo:
                    return fieldInfo.GetValue(obj);
                case PropertyInfo propertyInfo:
                    return propertyInfo.GetValue(obj);
                default:
                    throw new InvalidOperationException();
            }
        }

Key=Items
Value=[{
 "Name": "Ballpen",
 "Price": 12.3
}, {
 "Name": "Pencil",
 "Price": 3.21
}]
Type=Array


Key=Date
Value="21112019"
Type=String
StringValue=21112019
grnmeado7
  • 31
  • 4