Home >Backend Development >C++ >How to Map Nested JSON Properties to Class Properties in C#?

How to Map Nested JSON Properties to Class Properties in C#?

Barbara Streisand
Barbara StreisandOriginal
2025-01-24 23:06:10507browse

How to Map Nested JSON Properties to Class Properties in C#?

Map the sub -attributes of the json object to the class attribute

When encountering JSON data with a layered structure, you may want to map the sub -attributes of JSON complex objects to the simple attributes in the class. Although the newTonsoft.json framework provides

attributes to mappore the original JSON data, it does not support mapping attributes.

[DataMember] However, there are some ways to achieve this mapping. A simple method is to sequence JSON back to

and use

to retrieve the required sub -attributes. JObject SelectToken For example, consider the following JSON data:

To map the
<code class="language-json">{
    "picture": {
        "id": 123456,
        "data": {
            "type": "jpg",
            "url": "http://www.someplace.com/mypicture.jpg"
        }
    }
}</code>
attributes of sub -attributes to the class, you can use the following code:

url ProfilePicture If you need a more advanced solution, you can customize

to enable
<code class="language-csharp">string json = @"
{
    ""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>
attributes to work as expected. This converter will fill all attributes with the path specified in the attribute.

JsonConverter For example, consider the following JSON data: JsonProperty

To deserture JSON and map the sub -attributes to the simple attributes in the class, you can use the following custom converter:

<code class="language-json">{
  "name": "Joe Shmoe",
  "age": 26,
  "picture": {
    "id": 123456,
    "data": {
      "type": "jpg",
      "url": "http://www.someplace.com/mypicture.jpg"
    }
  },
  "favorites": {
    "movie": {
      "title": "The Godfather",
      "starring": "Marlon Brando",
      "year": 1972
    },
    "color": "purple"
  }
}</code>

With a custom converter, you can recal the json as usual as usual:

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

    [JsonProperty("age")]
    public int Age { get; set; }

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

    [JsonProperty("favorites.movie")]
    public Movie FavoriteMovie { get; set; }

    [JsonProperty("favorites.color")]
    public string FavoriteColor { get; set; }
}</code>

The above is the detailed content of How to Map Nested JSON Properties to Class Properties in C#?. 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