You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flurl GetJsonAsync反序列化Cosmos SDK JSON失败求助

问题分析与解决方案

核心问题点

  1. 反序列化接口报错:直接用GetJsonAsync<IAccount>()报错,是因为JSON序列化器无法实例化接口/抽象类,这是序列化的基本规则。
  2. 反序列化BaseAccount返回空值:原因有三个:
    • JSON最外层包了account节点,而你直接把BaseAccount作为根对象反序列化,字段完全不匹配
    • BaseAccount的Type属性标注的JsonProperty是type,但JSON里对应的字段是@type,映射失败
    • JSON中account_number和sequence是字符串类型,而代码里定义的是ulong,类型不兼容导致反序列化失败

具体修复步骤

1. 定义JSON根对象类

先创建对应整个JSON结构的根类,匹配外层的account节点:

public class AccountResponse
{
    [JsonProperty(PropertyName = "account")]
    public BaseAccount Account { get; set; } = null!;
}

2. 修正BaseAccount的字段映射与类型转换

调整字段的JsonProperty匹配JSON,同时处理字符串转ulong的问题:

public class BaseAccount : IAccount
{
    public BaseAccount()
    {
    }
    public BaseAccount(string type, string address, PublicKey publicKey, ulong accountNumber, ulong sequence)
    {
        Type = type;
        Address = address;
        PublicKey = publicKey;
        AccountNumber = accountNumber;
        Sequence = sequence;
    }

    // 修正为@type,匹配JSON中的字段名
    [JsonProperty(PropertyName = "@type")]
    public string Type { get; set; } = null!;

    // 用转换器处理字符串转ulong
    [JsonProperty(PropertyName = "account_number")]
    [JsonConverter(typeof(StringToUlongConverter))]
    public ulong AccountNumber { get; set; }

    [JsonProperty(PropertyName = "address")]
    public string Address { get; set; } = null!;

    [JsonProperty(PropertyName = "pub_key")]
    public PublicKey PublicKey { get; set; } = null!;

    // 同样用转换器处理sequence字段
    [JsonProperty(PropertyName = "sequence")]
    [JsonConverter(typeof(StringToUlongConverter))]
    public ulong Sequence { get; set; }

    // 接口实现保持原有逻辑
    public PublicKey GetPublicKey()
    {
        return new PublicKey()
        {
            Type = null,
            Key = PublicKey.Key
        };
    }
    public ulong GetSequence() => Sequence;
    public ulong GetAccountNumber() => AccountNumber;
}

3. 实现字符串转ulong的转换器

处理JSON中字符串类型的数字到ulong的转换:

public class StringToUlongConverter : JsonConverter<ulong>
{
    public override ulong ReadJson(JsonReader reader, Type objectType, ulong existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        if (reader.Value is string str)
        {
            ulong.TryParse(str, out var result);
            return result;
        }
        return existingValue;
    }

    public override void WriteJson(JsonWriter writer, ulong value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString());
    }
}

4. 定义PublicKey类

确保与JSON中的pub_key结构匹配:

public class PublicKey
{
    [JsonProperty(PropertyName = "@type")]
    public string? Type { get; set; }

    [JsonProperty(PropertyName = "key")]
    public string Key { get; set; } = null!;
}

5. 正确执行反序列化

现在用根对象类来反序列化,就能拿到完整的Account数据:

var accountResponse = await clientResponse.GetJsonAsync<AccountResponse>();
BaseAccount account = accountResponse.Account;

内容的提问来源于stack exchange,提问作者Adonis Villanueva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 13:17:32