Read value from dynamic property from json payload

前端 未结 2 914
情话喂你
情话喂你 2021-01-23 12:22

I have a simple model that I used as a request payload

public class CommandRequest
{
    public CommandType Type { get; set; }
    public dynamic Attributes { get         


        
相关标签:
2条回答
  • 2021-01-23 12:54

    Simply declaring something as dynamic doesn't guarantee that the resulting concrete object implements IDynamicMetaObjectProvider and allows for runtime definition of properties. Rather, dynamic simply means an object to which all compile-time checking has been turned off, and so all method and member references to it will be resolved in runtime. See:

    • What Is Dynamic?
    • What is the 'dynamic' type in C# 4.0 used for?

    Now, when you deserialize a JSON object to a member declared as dynamic with Json.NET, Newtonsoft will chose JObject as the concrete type to which to deserialize. As its base type JToken implements IDynamicMetaObjectProvider you can do things like requestBody.Attributes.Name and the .Net runtime will forward the property resolution to the JObject which will look the property up inside its list of properties. However, this doesn't happen automatically, Newtonsoft had to enhance JToken to make dynamic property access possible.

    System.Text.Json, however, does not have built-in support for deserializing free-form JSON to some custom type implementing IDynamicMetaObjectProvider, so you will need to use the compile-time methods of the actual type returned, viz. JsonElement, to access the JSON data contained therein:

    var name = requestBody.Attributes.GetProperty("Name").ToString();
    

    Or, you could cast it for clarity:

    var name = ((JsonElement)requestBody.Attributes).GetProperty("Name").ToString();
    

    Demo fiddle here.

    0 讨论(0)
  • 2021-01-23 13:06
    1. You can use the GetProperty method on the JsonElement to get the property you desire.
    2. You can deserialize with JsonConvert.Deseralize("jsonpayload") to the model of Attributes and get Name directly with the "." notation.
    3. You can deserialize to Dictionary<string,string> and get the property name as Dict["Name"].
    0 讨论(0)
提交回复
热议问题