Json.NET可以反序列化“动态”属性吗?

时间:2022-04-05 17:06:40

my C# service is receiving objects from an external service with a "fuzzy" format that looks like:

我的C#服务正在从外部服务接收具有“模糊”格式的对象,如下所示:

{
  "member": {
      "<dynamicProperty>": {
          "value":"some_string",
          "score": 10
      }
}

This property "" can change for every object, I don't have a defined and restricted list for its possible values and of course I can't change this format.

此属性“”可以针对每个对象进行更改,我没有针对其可能值的已定义和受限列表,当然我无法更改此格式。

Does anybody know if Json.NET or some other Json .NET serializer, could help me and allow me to define classes like Member and DynamicProperty below that I could use for an easy deserialization?

有没有人知道Json.NET或其他一些Json .NET序列化程序是否可以帮助我并允许我在下面定义类似Member和DynamicProperty的类,我可以使用它来进行简单的反序列化?

class Member
{
    [JsonProperty(PropertyName= "??")] // what should I put here?
    public DynamicProperty { get; set; }
}

class DynamicProperty
{
    public string value;
    public int score;
}

Thanks

1 个解决方案

#1


6  

You can use a Dictionary<string, object>

您可以使用Dictionary ,object>

class Member
{
     public Dictionary<string, object> { get; set; }
}

Or, you can use the JsonExtensionData Attribute:

或者,您可以使用JsonExtensionData属性:

class Member
{
     [JsonExtensionData]
     public Dictionary<string, JToken> { get; set; }
}

#1


6  

You can use a Dictionary<string, object>

您可以使用Dictionary ,object>

class Member
{
     public Dictionary<string, object> { get; set; }
}

Or, you can use the JsonExtensionData Attribute:

或者,您可以使用JsonExtensionData属性:

class Member
{
     [JsonExtensionData]
     public Dictionary<string, JToken> { get; set; }
}