【发布时间】:2018-01-17 08:52:16
【问题描述】:
我在正确反序列化十进制值时遇到问题。网站上的一个建议是使用构造函数,但它没有调用该构造函数。
这是 JSON:
{
"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; }
}
我用这个来称呼它:
var result = JsonConvert.DeserializeObject<ApiPagingResponse<GetBalanceListResponse>>(stringResult);
stringResult 是我要反序列化的 json 字符串。
目前它只为响应的 Entity 属性返回 null。我所有的其他序列化都可以用这种方法正常工作,问题出在"balance": 1e-8,
有没有人处理过类似的问题并且可以提供帮助?
【问题讨论】:
-
在
GetBalanceListResponse中,public GetBalanceListResponseEntity Entity { get; set; }应该是public List<GetBalanceListResponseEntity> Entities { get; set; }。样品fiddle。否则你的代码基本上可以工作。 -
@dbc 现在我觉得自己像个白痴
-
@dbc 如果您可以将此添加为答案,我可以将其标记为已接受,谢谢
-
已按要求添加答案。