【问题标题】:Newtonsoft DeserializeObject not calling constructorNewtonsoft DeserializeObject 不调用构造函数
【发布时间】: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&lt;GetBalanceListResponseEntity&gt; Entities { get; set; }。样品fiddle。否则你的代码基本上可以工作。
  • @dbc 现在我觉得自己像个白痴
  • @dbc 如果您可以将此添加为答案,我可以将其标记为已接受,谢谢
  • 已按要求添加答案。

标签: c# json json.net


【解决方案1】:

您对[JsonConstructor] 的使用是正确的。您唯一的问题是,在GetBalanceListResponse 中,方法

public GetBalanceListResponseEntity Entity { get; set; } 

应该是

public List<GetBalanceListResponseEntity> Entities { get; set; }

这是因为,在 JSON 中,对应的属性response.entities 是一个数组:

{
    "errors": false,
    "response": {
        "entities": [
            // Entity values omitted
        ]
    },
    // Pagination omitted
}

通过这个修复,构造函数被调用并且你的代码基本上可以工作了。样品fiddle

为避免将来出现此类问题,您可以使用自动代码生成工具(例如 http://json2csharp.com/Paste JSON as Classeshttps://jsonutils.com/)从 JSON 生成适当的类,然后根据需要进行修改,例如通过使自动生成的类型成为通用类型。

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多