JSON反序列化对象返回null
源代码 - 主类
string responseBody = await response.Content.ReadAsStringAsync();
status.result deserializeObject = JsonConvert.DeserializeObject<status.result>(responseBody);
Debug.WriteLine(deserializeObject.SafeGasPrice.ToString());
源代码 - JSON 类
public class status
{
public class result
{
[JsonProperty(PropertyName = "SafeGasPrice")]
public int SafeGasPrice { get; set; }
[JsonProperty(PropertyName = "ProposeGasPrice")]
public int ProposeGasPrice { get; set; }
[JsonProperty(PropertyName = "FastGasPrice")]
public int FastGasPrice { get; set; }
}
}
输出
{"status":"1","message":"OK","result":{"LastBlock":"14296250","SafeGasPrice":"96","ProposeGasPrice":"96","FastGasPrice":"97","suggestBaseFee":"95.407119606","gasUsedRatio":"0.174721033333333,0.523179548504219,0.056945596868572,0.999939743363228,0.953861217484817"}}
0
问题
我目前不明白为什么输出 null,我的猜测是我错误地实现了 json 反序列化类。
Source Code - Main class
string responseBody = await response.Content.ReadAsStringAsync();
status.result deserializeObject = JsonConvert.DeserializeObject<status.result>(responseBody);
Debug.WriteLine(deserializeObject.SafeGasPrice.ToString());
Source Code - JSON Class
public class status
{
public class result
{
[JsonProperty(PropertyName = "SafeGasPrice")]
public int SafeGasPrice { get; set; }
[JsonProperty(PropertyName = "ProposeGasPrice")]
public int ProposeGasPrice { get; set; }
[JsonProperty(PropertyName = "FastGasPrice")]
public int FastGasPrice { get; set; }
}
}
Output
{"status":"1","message":"OK","result":{"LastBlock":"14296250","SafeGasPrice":"96","ProposeGasPrice":"96","FastGasPrice":"97","suggestBaseFee":"95.407119606","gasUsedRatio":"0.174721033333333,0.523179548504219,0.056945596868572,0.999939743363228,0.953861217484817"}}
0
Problem
I don't currently understand why a null is output, my guess is that I have implemented the json deserialization classes incorrectly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的数据模型与提供的 JSON 不对应,它缺少与外部
{"result": { }}
对象相对应的类型:要解决此问题,您需要引入一个外部,包装模型。您可以像这样明确地进行:
并像这样反序列化:
或者,您可以使用
无论哪种方式,您现在都可以成功反序列化内部嵌套属性。
那么你做错了什么?在您的问题中,您将
result
声明为 嵌套类型:这一切都是在另一个类型
status<范围内定义一个类型
result
/代码>。它不会在status
中创建名为result
的属性。由于不需要此类嵌套,我建议将result
从status
内部移出,并将其重命名为Result
以遵循标准 .NET 命名约定。演示小提琴此处。
Your data model does not correspond to the JSON provided, it is missing a type corresponding to the outer
{"result": { }}
object:To work around the problem, you need to introduce an outer, wrapper model. You could make an explicit one like so:
And deserialize like so:
Or, you could use an anonymous type for the root model like so:
Either way you will now be able to successfully deserialize the inner, nested properties.
So what did you do wrong? In your question, you declare
result
as a nested type:All this does is define a type
result
within the scope of another typestatus
. It does not create a property namedresult
withinstatus
. As there is no need for such nesting I recommend movingresult
out from insidestatus
and renaming itResult
to follow standard .NET naming conventions.Demo fiddle here.