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; }
}
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:
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.
发布评论
评论(2)
你的反序列化类是错误的。尝试这个
课程
you have a wrong class for deserialization. try this
classes
您的数据模型有两个问题。
层次结构
数据模型的第一个问题是它没有对 json 进行建模。在你的 json 中你有一个层次结构。如果您通过在线 jsonformatter 格式化 json,那么您应该看到类似这样的内容
status
、message
和result
LastBlock
, ... ,FastGasPrice
在result
节点下定义。您的模型也应该代表此层次结构。有一些网站(例如 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
,message
andresult
LastBlock
, ... ,FastGasPrice
are defined under theresult
nodeYour 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 thoughDeserializeObject
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 accessstatic
member through an instance object.