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
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.
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?