JSON反序列化对象返回null

发布于 2025-01-11 00:49:10 字数 1124 浏览 1 评论 0原文

源代码 - 主类

        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 技术交流群。

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

发布评论

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

评论(1

夜空下最亮的亮点 2025-01-18 00:49:10

您的数据模型与提供的 JSON 不对应,它缺少与外部 {"result": { }} 对象相对应的类型:

{
   "status":"1",
   "message":"OK",
   "result":{
      // This inner object corresponds to your model.
      "LastBlock":"14296250",
      "SafeGasPrice":"96",
      "ProposeGasPrice":"96",
      "FastGasPrice":"97",
      "suggestBaseFee":"95.407119606",
      "gasUsedRatio":"0.174721033333333,0.523179548504219,0.056945596868572,0.999939743363228,0.953861217484817"
   }
}

要解决此问题,您需要引入一个外部,包装模型。您可以像这样明确地进行:

public class Root
{
    public string status { get; set; }
    public string message { get; set; }
    public Result result { get; set; }
}

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; }
}

并像这样反序列化:

var deserializeObject = JsonConvert.DeserializeObject<Root>(responseBody)?.result;

或者,您可以使用

var deserializeObject = JsonConvert.DeserializeAnonymousType(responseBody, new { result = default(Result) })?.result;

无论哪种方式,您现在都可以成功反序列化内部嵌套属性。

那么你做错了什么?在您的问题中,您将 result 声明为 嵌套类型

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<范围内定义一个类型result /代码>。它不会在 status 中创建名为 result属性。由于不需要此类嵌套,我建议将 resultstatus 内部移出,并将其重命名为 Result 以遵循标准 .NET 命名约定。

演示小提琴此处

Your data model does not correspond to the JSON provided, it is missing a type corresponding to the outer {"result": { }} object:

{
   "status":"1",
   "message":"OK",
   "result":{
      // This inner object corresponds to your model.
      "LastBlock":"14296250",
      "SafeGasPrice":"96",
      "ProposeGasPrice":"96",
      "FastGasPrice":"97",
      "suggestBaseFee":"95.407119606",
      "gasUsedRatio":"0.174721033333333,0.523179548504219,0.056945596868572,0.999939743363228,0.953861217484817"
   }
}

To work around the problem, you need to introduce an outer, wrapper model. You could make an explicit one like so:

public class Root
{
    public string status { get; set; }
    public string message { get; set; }
    public Result result { get; set; }
}

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; }
}

And deserialize like so:

var deserializeObject = JsonConvert.DeserializeObject<Root>(responseBody)?.result;

Or, you could use an anonymous type for the root model like so:

var deserializeObject = JsonConvert.DeserializeAnonymousType(responseBody, new { result = default(Result) })?.result;

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:

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; }
    }
}

All this does is define a type result within the scope of another type status. It does not create a property named result within status. As there is no need for such nesting I recommend moving result out from inside status and renaming it Result to follow standard .NET naming conventions.

Demo fiddle here.

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