【问题标题】:Deserialize scientific notation number with RestSharp使用 RestSharp 反序列化科学计数法数字
【发布时间】:2021-07-04 22:58:30
【问题描述】:

我从 API 接收到一个包含以下属性的对象

{
    "property": {
        "eth": 2.554e-05
    }
}

现在,当 RestSharp 反序列化我的对象时

var response = await restClient.ExecuteAsync<TokenPrice>(restRequest);

我的 c# 对象的 Eth 属性将设置为 0。我尝试将该属性修改为字符串(它将设置为 null)、小数和双精度,但我一直得到 0。

我怎样才能像这样将该数字反序列化到我的对象中

public class TokenPrice
{
    public decimal Eth { get; set; } -> eth = 0.00002566
}

?谢谢

【问题讨论】:

  • 您的 C# 对象似乎缺少 JSON 的“属性”部分。您需要将 TokenPrice 包装在带有 public TokenPrice Property {get;set;} 的类中,然后反序列化为该包装类。

标签: c# .net deserialization restsharp


【解决方案1】:

对于问题中的请求,您需要具有如下所示的类型结构

public class Price
{
    public decimal eth { get; set; }
}

public class Root
{
    public Price property { get; set; }
}

然后

var response = await restClient.ExecuteAsync<Root>(restRequest);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多