我如何从由Jil.JSON.DeserializeDynamic创建的动态对象中获得非公开成员?

问题描述 投票:1回答:1

我是Jil.JSON的新手。我使用的是'JSON.DeserializeDynamic',因为'JSON'字符串总是因不同的客户需求而有所不同。

在我的代码中,我正在使用'foreach'。 'keyValue'是具有'Key'和'Value'属性的动态。

我正在尝试获取'keyValue.Value类型'和'keyValue.Value StringValue'的值,但我不知道如何获取这些非公共成员的值。

需要获取类型和StringValue

enter image description here

这里是我的代码示例:

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     
       };
}

我尝试使用'keyValue.GetType()',但它给了我不同的属性。

enter image description here

编辑:

我快到了。...

            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

现在,我需要获取'会员价值'

任何想法?

c# jil
1个回答
0
投票

...不是一个好的代码,但现在可以正常工作。

...
           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();
            }
...

我需要向下转换为FieldInfo或PropertyInfo以获取成员的值

        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

推荐问答