Home >Backend Development >C++ >Can I Map Child JSON Properties to Class Properties Using Attributes?

Can I Map Child JSON Properties to Class Properties Using Attributes?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-24 23:02:14345browse

Can I Map Child JSON Properties to Class Properties Using Attributes?

Use attributes to access sub-properties in JSON?

JSON deserialization using Newtonsoft.Json's DeserializeObject<T> allows mapping raw JSON data to class properties using [DataMember(Name = "raw_property_name")]. But what if you want to map a sub-property of a complex JSON object to a simple property in a class?

For example, consider the following JSON:

<code class="language-json">{
    "picture": 
    {
        "id": 123456,
        "data": 
        {
            "type": "jpg",
            "url": "http://www.someplace.com/mypicture.jpg"
        }
    }
}</code>

You are probably only interested in the picture properties of the url object, so you don't want to create complex objects in your class. Can you simply map it like this?

<code class="language-csharp">[DataMember(Name = "picture.data.url")]
public string ProfilePicture { get; set; }</code>

Use JObject for direct path mapping

A simple way is to parse the JSON into JObject. Then, use ToObject() to populate your class from JObject . To extract additional properties, use SelectToken():

<code class="language-csharp">string json = @"
{
    ""name"" : ""Joe Shmoe"",
    ""age"" : 26,
    ""picture"":
    {
        ""id"": 123456,
        ""data"":
        {
            ""type"": ""jpg"",
            ""url"": ""http://www.someplace.com/mypicture.jpg""
        }
    }
}";

JObject jo = JObject.Parse(json);
Person p = jo.ToObject<Person>();
p.ProfilePicture = (string)jo.SelectToken("picture.data.url");</code>

Custom JsonConverter for property based mapping

Alternatively, you can create a custom JsonConverter to enable the JsonProperty attribute to map sub-properties according to your needs:

<code class="language-csharp">class JsonPathConverter : JsonConverter
{
    // ... 实现代码在问题答案中提供 ...
}</code>

How to use it:

<code class="language-csharp">[JsonConverter(typeof(JsonPathConverter))]
class Person
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("picture.data.url")]
    public string ProfilePicture { get; set; }

    // ... 其他属性 ...
}

Person p = JsonConvert.DeserializeObject<Person>(json);</code>

This allows you to use attribute-based mapping for sub-properties, providing greater flexibility when deserializing JSON data into class objects.

The above is the detailed content of Can I Map Child JSON Properties to Class Properties Using Attributes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn