【问题标题】:Deserializing JSON with numbers as key以数字为键反序列化 JSON
【发布时间】:2020-10-27 14:47:17
【问题描述】:

我有这样的 JSON:

{
  "success": 1,
  "method": "getTrades",
  "5183": {
    "buy_amount": "0.00455636",
    "buy_currency": "BTC"
  },
  "6962": {
    "buy_amount": "52.44700000",
    "buy_currency": "IOT"
  },
  "6963": {
    "buy_amount": "383.54300000",
    "buy_currency": "TNT"
  },
  "6964": {
    "buy_amount": "3412.50000000",
    "buy_currency": "FUN"
  },
  "6965": {
    "buy_amount": "539.45000000",
    "buy_currency": "XLM"
  }
}

我需要将此 JSON 转换为域模型。

      public class Root {
       [JsonProperty("success")]
       public long Success { get; set; }
       [JsonProperty("method")]
       public string Method { get; set; }
       public Dictionary<long, Transaction> Transactions { get; set; }

      public class Transaction{
       [JsonProperty("buy_amount")]
       public decimal? BuyAmount { get; set; }
       [JsonProperty("buy_currency")]
       public string BuyCurrency { get; set; }

      var model = JsonConvert.DeserializeObject<Root>(response);

我创建了这两个模型,但是当我尝试反序列化事务时,它们为空。我得到了成功和方法的数据

【问题讨论】:

  • 这是因为所有这些 JSON 属性都与您的 success 属性处于同一级别,而不是在 Transactions 属性中。
  • 我应该改变什么?我不想为所有事务创建类。
  • 一种方法是将Transactions 属性重写为:[JsonExtensionData] public Dictionary&lt;string, JToken&gt; Transactions { get; } = new Dictionary&lt;string, JToken&gt;();,但之后您必须转换每个这样的对。我不知道有什么更好的方法。

标签: c# json deserialization


【解决方案1】:

要么 json 需要改变,要么你需要修改你的 c# 结构。不是 100% 确定,但是基于 JSON,c# 类应该是

public class Root : Dictionary<long, Transaction> {
   [JsonProperty("success")]
   public long Success { get; set; }
   [JsonProperty("method")]
   public string Method { get; set; }

【讨论】:

  • 那行不通,因为success 不是Transaction。显然,它并没有首先检查是否存在可以将值存储到的特定属性,而是尝试将所有内容都塞入字典部分。我试过了,给出异常。
【解决方案2】:

你能把 JSON 改成这样吗?

{
   "success": 1,
   "method": "getTrades",
   "transaction": [
      {
         "id": 5183,
         "buy_amount": "0.00455636",
         "buy_currency": "BTC"
      },
      {
         "id": 6962,
         "buy_amount": "52.44700000",
         "buy_currency": "IOT"
      },
      {
         "id": 6963,
         "buy_amount": "383.54300000",
         "buy_currency": "TNT"
      },
      {
         "id": 6964,
         "buy_amount": "3412.50000000",
         "buy_currency": "FUN"
      },
      {
         "id": 6965,
         "buy_amount": "539.45000000",
         "buy_currency": "XLM"
      }
   ]
}

然后你可以这样做:

    public class Transaction    {
        public int id { get; set; } 
        public string buy_amount { get; set; } 
        public string buy_currency { get; set; } 
    }

    public class Root    {
        public int success { get; set; } 
        public string method { get; set; } 
        public List<Transaction> transaction { get; set; } 
    }

【讨论】:

  • 我知道,但我无法改变。该 JSON 是来自某些 API 的响应。
  • 所有交易是否总是在响应中?还是视情况而定,一个或另一个可能不会来?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多