Home >Backend Development >C++ >How to Deserialize JSON with Dynamic Numeric Keys Using Newtonsoft.Json?
When dealing with complex JSON structures containing sub-objects with dynamic key names, deserializing them using Newtonsoft JSON.NET can present challenges. This article will delve into scenarios involving such structures and provide a complete solution using a custom JSON converter.
Consider the following JSON structure:
<code class="language-json">{ "users" : { "parentname":"test", "100034" : { "name" : "tom", "state" : "WA", "id" : "cedf-c56f-18a4-4b1" }, "10045" : { "name" : "steve", "state" : "NY", "id" : "ebb2-92bf-3062-7774" }, "12345" : { "name" : "mike", "state" : "MA", "id" : "fb60-b34f-6dc8-aaf7" } } }</code>
In this structure, the "users" object contains a mix of known properties ("parentname") and unknown properties (with numeric keys representing child objects). The goal is to deserialize this structure into a C# object model, where sub-objects are represented as strongly typed classes.
An initial attempt to deserialize JSON using standard JSON.NET code might look like this:
<code class="language-csharp">class RootObject { public string ParentName { get; set; } public Dictionary<string, User> users { get; set; } } class User { public string name { get; set; } public string state { get; set; } public string id { get; set; } }</code>
Deserialization to this object model fails due to unknown properties in the "users" object. JSON.NET by default expects property names to match class properties, and since the numeric key does not correspond to any property in the User class, the deserialization process fails.
To solve this problem, a custom JSON converter is required. TypedExtensionDataConverter
Elegantly solves this problem. Here is the code:
<code class="language-csharp">public class TypedExtensionDataConverter<T> : JsonConverter { // ... [此处省略实现,篇幅原因] }</code>
This converter allows deserializing unknown properties into a typed container, in this case a dictionary of User objects:
<code class="language-csharp">[JsonConverter(typeof(TypedExtensionDataConverter<Users>))] class Users { public Users() { this.UserTable = new Dictionary<string, User>(); } [JsonProperty("parentname")] public string ParentName { get; set; } [JsonTypedExtensionData] public Dictionary<string, User> UserTable { get; set; } }</code>
By using JsonTypedExtensionDataAttribute
, the converter knows to serialize/deserialize unknown properties into the UserTable
dictionary.
With a custom converter, the complete solution looks like this:
<code class="language-csharp">class RootObject { [JsonProperty("users")] public Users Users { get; set; } } [JsonConverter(typeof(TypedExtensionDataConverter<Users>))] class Users { // ... [如上实现] } class User { // ... [不变] }</code>
The deserialized JSON structure will now populate RootObject
with the expected values, including sub-objects stored in the Users
dictionary within the UserTable
object.
The above is the detailed content of How to Deserialize JSON with Dynamic Numeric Keys Using Newtonsoft.Json?. For more information, please follow other related articles on the PHP Chinese website!