Home > Backend Development > C++ > How to Deserialize a JSON Array of Mixed-Type Values into Strongly Typed C# Objects?

How to Deserialize a JSON Array of Mixed-Type Values into Strongly Typed C# Objects?

Barbara Streisand
Release: 2025-01-24 04:21:38
Original
996 people have browsed it

How to Deserialize a JSON Array of Mixed-Type Values into Strongly Typed C# Objects?

Deserialize an array of mixed type values ​​into strongly typed data

When dealing with JSON data with a specific schema, deserializing it into strongly typed data classes can improve code maintainability and clarity. This question explores how to deserialize such data when the array contains mixed type values, specifically integers and strings.

Key Considerations

There are two key factors to consider when deserializing this type of data:

  1. Handling player collections: Player data is indexed by username and contains a mix of strings and integers. This suggests that it is possible to use a dictionary structure with the username as the key and a collection of values ​​representing the data for each player.
  2. Unnamed player properties: Player data consists of a specific sequence of unnamed values. We need a mechanism to assign these values ​​to the properties of the Player object, ensuring that they map to the correct properties.

Custom converter implementation

To solve these problems, we can implement a custom deserialization converter. This converter is implemented in C#, utilizing the ObjectToArrayConverter<T> class in Newtonsoft.Json:

<code class="language-csharp">public class ObjectToArrayConverter<T> : JsonConverter
{
    // ... (为简洁起见,省略实现细节)
}</code>
Copy after login

Player class definition

Next, we define the Player class and use annotated properties to specify the order in which it is deserialized:

<code class="language-csharp">[JsonConverter(typeof(ObjectToArrayConverter<Player>))]
public class Player
{
    [JsonProperty(Order = 1)]
    public int UniqueID { get; set; }

    [JsonProperty(Order = 2)]
    public string PlayerDescription { get; set; }

    // ... (根据需要添加其他属性)
}</code>
Copy after login

Top-level object structure

Finally, the top-level ScoreboardResults class should be adjusted to represent player data as a dictionary with usernames as keys:

<code class="language-csharp">public class ScoreboardResults
{
    public int timestamp { get; set; }
    public int total_players { get; set; }
    public int max_score { get; set; }
    public Dictionary<string, Player[]> players { get; set; }
}</code>
Copy after login

Deserialization demo

Using custom converters and annotated properties, we can now deserialize JSON data into strongly typed objects:

<code class="language-json">{
    "timestamp": 1473730993,
    "total_players": 945,
    "max_score": 8961474,
    "players": {
            "Player1Username": [
            121,
            "somestring",
            679900,
            5,
            4497,
            "anotherString",
            "thirdString",
            "fourthString",
            123,
            22,
            "YetAnotherString"],
        "Player2Username": [
            886,
            "stillAstring",
            1677,
            1,
            9876,
            "alwaysAstring",
            "thirdString",
            "fourthString",
            876,
            77,
            "string"]
        }
}</code>
Copy after login
The

result will be a filled ScoreboardResults object containing a dictionary of Player objects, each containing the expected values ​​in their respective properties.

This revised output maintains the original image and provides a more concise and technically accurate explanation of the JSON deserialization process. The code examples are also improved for clarity.

The above is the detailed content of How to Deserialize a JSON Array of Mixed-Type Values into Strongly Typed C# Objects?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template