C# Newtonsoft反序列化对象未调用构造函数
我在正确反序列化十进制值方面遇到问题。网站上的一个建议是使用一个构造函数,但它没有调用该构造函数 以下是JSON:C# Newtonsoft反序列化对象未调用构造函数,c#,json,json.net,C#,Json,Json.net,我在正确反序列化十进制值方面遇到问题。网站上的一个建议是使用一个构造函数,但它没有调用该构造函数 以下是JSON: { "errors": false, "response": { "entities": [ { "currency_id": "1", "balance": 1e-8, "address": "" }, {
{
"errors": false,
"response": {
"entities": [
{
"currency_id": "1",
"balance": 1e-8,
"address": ""
},
{
"currency_id": "2",
"balance": 0,
"address": null
},
{
"currency_id": "3",
"balance": 0.09865566,
"address": null
},
{
"currency_id": "5",
"balance": 0,
"address": null
},
{
"currency_id": "6",
"balance": 0,
"address": null
}]
},
"pagination": {
"items_per_page": 100,
"total_items": 5,
"current_page": 1,
"total_pages": 1
}
}
我的班级:
public class ApiResponse<T> where T : class
{
public bool Errors { get; set; }
public T Response { get; set; }
}
public class ApiPagingResponse<T> : ApiResponse<T> where T : class
{
public Pagination Pagination { get; set; }
}
public class GetBalanceListResponse
{
public GetBalanceListResponseEntity Entity { get; set; }
}
[JsonObject]
public class GetBalanceListResponseEntity
{
[JsonConstructor]
public GetBalanceListResponseEntity([JsonProperty("currency_id")]string currencyId, [JsonProperty("balance")]string balance, [JsonProperty("address")]string address)
{
CurrencyId = currencyId;
Balance = decimal.Parse(balance, NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint,
CultureInfo.InvariantCulture);
Address = address;
}
[JsonProperty("currency_id")]
public string CurrencyId { get; set; }
[JsonProperty("balance")]
public decimal Balance { get; set; }
[JsonProperty("address")]
public string Address { get; set; }
}
公共类ApiResponse,其中T:class
{
公共布尔错误{get;set;}
公共T响应{get;set;}
}
公共类ApiPagingResponse:ApiResponse,其中T:class
{
公共分页分页{get;set;}
}
公共类GetBalanceListResponse
{
公共GetBalanceListResponseEntity{get;set;}
}
[JsonObject]
公共类GetBalanceListResponseEntity
{
[JsonConstructor]
public GetBalanceListResponseEntity([JsonProperty(“currency_id”)]字符串currencyId、[JsonProperty(“balance”)]字符串余额、[JsonProperty(“address”)]字符串地址)
{
CurrencyId=CurrencyId;
Balance=decimal.Parse(Balance,NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint,
文化信息(不变量文化);
地址=地址;
}
[JsonProperty(“货币id”)]
公共字符串CurrencyId{get;set;}
[JsonProperty(“余额”)]
公共十进制余额{get;set;}
[JsonProperty(“地址”)]
公共字符串地址{get;set;}
}
我这样称呼它:
var result = JsonConvert.DeserializeObject<ApiPagingResponse<GetBalanceListResponse>>(stringResult);
var result=JsonConvert.DeserializeObject(stringResult);
其中stringResult
是我要反序列化的json字符串
当前,它只为响应的实体属性返回null。我的所有其他序列化都可以使用此方法,问题在于“balance”:1e-8,
是否有人处理过类似的问题,并能对此提供帮助?您使用的是正确的。您唯一的问题是,在GetBalanceListResponse
中
public GetBalanceListResponseEntity Entity { get; set; }
应该是
public List<GetBalanceListResponseEntity> Entities { get; set; }
使用此修复程序,将调用构造函数,代码基本正常工作。样品
为了避免将来出现此类问题,您可以使用自动代码生成工具,例如,或从JSON生成适当的类,然后根据需要进行修改,例如,将自动生成的类型设置为泛型。在
GetBalanceListResponse
中,公共GetBalanceListResponseEntity{get;set;}
应该是公共列表实体{get;set;}
。样品否则你的代码基本上可以正常工作。@dbc现在我觉得自己像一个完整的idiot@dbc如果你能把这个作为一个答案,我可以把它标记为接受,谢谢你按要求添加。
{
"errors": false,
"response": {
"entities": [
// Entity values omitted
]
},
// Pagination omitted
}