JSON.NET uses the default constructor to use the default constructor when the derivative object is. However, if you have a class with a default constructor and non -default constructor, you may encounter problems when trying to use non -default constructor functions for reference.
Use [JSONCONSTRUCTOR] Features:
To indicate that JSON.NET uses a non -default constructor, you can add the [JSONCONSTRUCTOR] feature. This characteristic must be placed on a constructor to be used for deepericularization.
Example:
Make sure the constructor parameter name matches the corresponding JSON attribute name (ignore the lower case).
Jsonconveter:<code class="language-csharp">[JsonConstructor] public Result(int? code, string format, Dictionary<string, string> details = null) { // 构造函数逻辑 }</code>
If you cannot modify the class or do not want to use the characteristics, you can create a custom JSONCONVERRER to manually process the derivative.
Example:
Finally, add the converter to the serialization program settings before the deepertdialization:
The above is the detailed content of How to Deserialize JSON Objects with Non-Default Constructors in JSON.net?. For more information, please follow other related articles on the PHP Chinese website!