反序列化的对象没有价值

发布于 2025-01-10 00:51:45 字数 1455 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巾帼英雄 2025-01-17 00:51:45

你的反序列化类是错误的。尝试这个

var deserializedObject = JsonConvert.DeserializeObject<Data>(responseBody);

课程

public partial class Data
    {
        [JsonProperty("status")]
        public long Status { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }

        [JsonProperty("result")]
        public Result Result { get; set; }
    }

    public partial class Result
    {
        [JsonProperty("LastBlock")]
        public long LastBlock { get; set; }

        [JsonProperty("SafeGasPrice")]
        public long SafeGasPrice { get; set; }

        [JsonProperty("ProposeGasPrice")]
         public long ProposeGasPrice { get; set; }

        [JsonProperty("FastGasPrice")]
        public long FastGasPrice { get; set; }

        [JsonProperty("suggestBaseFee")]
        public double SuggestBaseFee { get; set; }

        [JsonProperty("gasUsedRatio")]
        public string GasUsedRatio { get; set; }
    }

you have a wrong class for deserialization. try this

var deserializedObject = JsonConvert.DeserializeObject<Data>(responseBody);

classes

public partial class Data
    {
        [JsonProperty("status")]
        public long Status { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }

        [JsonProperty("result")]
        public Result Result { get; set; }
    }

    public partial class Result
    {
        [JsonProperty("LastBlock")]
        public long LastBlock { get; set; }

        [JsonProperty("SafeGasPrice")]
        public long SafeGasPrice { get; set; }

        [JsonProperty("ProposeGasPrice")]
         public long ProposeGasPrice { get; set; }

        [JsonProperty("FastGasPrice")]
        public long FastGasPrice { get; set; }

        [JsonProperty("suggestBaseFee")]
        public double SuggestBaseFee { get; set; }

        [JsonProperty("gasUsedRatio")]
        public string GasUsedRatio { get; set; }
    }
一江春梦 2025-01-17 00:51:45

您的数据模型有两个问题。

层次结构

数据模型的第一个问题是它没有对 json 进行建模。在你的 json 中你有一个层次结构。如果您通过在线 jsonformatter 格式化 json,那么您应该看到类似这样的内容

{
   "status":"1",
   "message":"OK",
   "result":{
      "LastBlock":"14276296",
      "SafeGasPrice":"65",
      "ProposeGasPrice":"65",
      "FastGasPrice":"67",
      "suggestBaseFee":"64.251201854",
      "gasUsedRatio":"0.999343233333333,0.308395633333333,0.161141266666667,0.843474233333333,0.501605648750662"
   }
}
  • :看到你有三个顶级节点: statusmessageresult
  • LastBlock, ... , FastGasPriceresult 节点下定义。

您的模型也应该代表此层次结构。有一些网站(例如 json2csharp)可以帮助您生成草稿 C# 数据类。

或者您也可以使用 Linq2Json 手动进行映射。

静态属性

另一个问题是您已将属性声明为static。尽管 DeserializeObject 可以处理它们,但很可能您不需要它们是静态的。

顺便说一句:deserializedObject.SafeGasPrice <<这应该会导致编译时错误,因为您无法通过实例对象访问static成员。

Your data model has two problems.

Hierarchy

The first problem with your data model is that it is not modelling your json. In your json you have a hierarchy. If you format your json for example via an online jsonformatter then you should see something like this:

{
   "status":"1",
   "message":"OK",
   "result":{
      "LastBlock":"14276296",
      "SafeGasPrice":"65",
      "ProposeGasPrice":"65",
      "FastGasPrice":"67",
      "suggestBaseFee":"64.251201854",
      "gasUsedRatio":"0.999343233333333,0.308395633333333,0.161141266666667,0.843474233333333,0.501605648750662"
   }
}
  • As you can see you have three top level nodes: status, message and result
  • LastBlock, ... , FastGasPrice are defined under the result node

Your models should represent this hierarchy as well. There are websites (like json2csharp) which can help you to generate draft C# data classes.

Or you can also use Linq2Json to do the mapping manually.

static properties

The other problem is that you have declared your properties as static. Even though DeserializeObject can deal with them, most probably you don't need them to be static.

By the way: deserializedObject.SafeGasPrice << This should cause compile time error since you can't access static member through an instance object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文