C# JSON Revitalization: bypassing the default structure function
When using JSON.NET to sequence JSON data into C#objects, it usually prefer to use the default constructor. However, if a non -default constructor is required, you can use the
attribute.
[JsonConstructor]
Use
[JsonConstructor]
By adding attributes to the non -default constructor, JSON.NET will use it to serialize. The constructive function parameter must match the name of the JSON attribute, and the case is not distinguished. The attributes that the constructive function parameters are not covered can still be filled by public interviewer or and other attributes.
[JsonConstructor]
Example: [JsonProperty]
Jsonconveter [JsonConstructor]
<code class="language-csharp">[JsonConstructor] public Result(int? code, string format, Dictionary<string, string> details = null) { ... }</code>
Then, add the converter to the JSON serialization program settings, and use it during the dependentization period:
JsonConverter
By using these technologies, even if there is a default constructor, you can also transform the JSON data to the object without relying on the default constructor.
The above is the detailed content of How to Deserialize JSON into C# Objects Without a Default Constructor?. For more information, please follow other related articles on the PHP Chinese website!