c# 报错提示:不支持对没有无参数构造函数、单个参数化构造函数或带‘JsonConstructorAttribute‘注释的参数化构造函数的类型进行反序列化。类型为
报错:
{“Deserialization of types without a parameterless constructor, a
singular parameterized constructor, or a parameterized constructor
annotated with ‘xxxxxx’ is not supported. Type ‘xxxxx’. Path: $ |
LineNumber: 0 | BytePositionInLine: 1.”}
报错翻译为中文:
不支持对没有无参数构造函数、单个参数化构造函数或带’JsonConstructorAttribute’注释的参数化构造函数的类型进行反序列化。类型为xxxxxx
问题原因:只有一个带参数的构造函数,没有无参构造函数。
解决方案:出于某种原因不想或不能提供一个无参数的构造函数,可以通过[JsonConstructor]属性来指定一个带参数的构造函数作为JSON反序列化的入口点。
增加 [JsonConstructor] 后报错
{"Each parameter in the deserialization constructor on type’XXXXX’
问题原因:只有一个带参数的构造函数,但构造函数参数不匹配。
反序列化构造函数(标记[JsonConstructor])的参数名称必须与JSON属性名称严格匹配(不区分大小写),或通过[JsonPropertyName]显式映射。
示例:若构造函数参数为oName,而JSON中属性为"name",会因名称不一致报错。
解决方案:参数名称和属性名称保持一致。
