【问题标题】:WhatToMine Json Coins Object to ListWhatToMine Json 硬币对象列表
【发布时间】:2017-12-24 21:35:58
【问题描述】:

我正在尝试在我的宠物 C# 项目中使用他们的 JSON 从WhatToMine 获得前 5 个最赚钱的挖矿币。

问题是这个站点返回的不是一个数组而是一个对象(为了简洁起见,我已经对属性列表进行了排序):

{
   "coins":
   {
      "Hush":
      {
         "id":168,
         "tag":"HUSH",
         "algorithm":"Equihash",
      },
      "Zclassic":
      {
         "id":167,
         "tag":"ZCL",
         "algorithm":"Equihash"
      }
}

我真的不需要硬币名称,因为标签就足够了,所以我想要这样的东西:

[
   {
      "id":168,
      "tag":"HUSH",
      "algorithm":"Equihash",
   },
   {
      "id":167,
      "tag":"ZCL",
      "algorithm":"Equihash"
   }
]

我尝试使用JSON2CSharp,但它生成了一堆具有相同属性的类,每个硬币都有一个。而且由于不断添加新代码,我不想每次都更改代码。

当然,我可以做一些搜索/替换或正则表达式来使 JSON 响应字符串看起来像我需要的那样,但我猜真正的开发人员(我不是其中的一部分)知道反序列化单个对象的更好和更优雅的方法进入列表/数组。

【问题讨论】:

  • 这里的答案之一将起作用:stackoverflow.com/questions/4535840/…
  • 类名无关紧要,因此您可以使用定义为 {id, tag, algo} 之类的 CoinItem 并将任何/所有类型映射到它 (CoinItem Hush...)
  • 谢谢你们,我之前看过那个文档和 Rene 的链接,但无法正常工作

标签: c# arrays json object deserialization


【解决方案1】:

除非您有非常具体的用例,否则我建议使用 Newtonsoft.Json 作为您事实上的 JSON 库。它会为您省去很多麻烦。

问题是这个站点返回的不是数组而是单个对象

将对象映射到Dictionary

尽可能选择自动序列化/反序列化:

using System.Collections.Generic;
using System.Net;

using Newtonsoft.Json;

namespace WhatToMine
{
    using MineBlob = Dictionary<string, Dictionary<string, CoinBlob>>;

    class CoinBlob
    {
        [JsonProperty(PropertyName = "id")]
        public int Id;
        [JsonProperty(PropertyName = "tag")]
        public string Tag;
        [JsonProperty(PropertyName = "algorithm")]
        public string Algorithm;
        [JsonProperty(PropertyName = "block_time")]
        public double BlockTime;
        [JsonProperty(PropertyName = "block_reward")]
        public double BlockReward;
        [JsonProperty(PropertyName = "block_reward24")]
        public double BlockReward24;
        [JsonProperty(PropertyName = "last_block")]
        public long LastBlock;
        [JsonProperty(PropertyName = "difficulty")]
        public double Difficulty;
        [JsonProperty(PropertyName = "difficulty24")]
        public double Difficulty24;
        [JsonProperty(PropertyName = "nethash")]
        public long NetHash;
        [JsonProperty(PropertyName = "exchange_rate")]
        public double ExchangeRate;
        [JsonProperty(PropertyName = "exchange_rate24")]
        public double ExchangeRate24;
        [JsonProperty(PropertyName = "exchange_rate_vol")]
        public double ExchangeRateVolume;
        [JsonProperty(PropertyName = "exchange_rate_curr")]
        public string ExchangeRateCurrency;
        [JsonProperty(PropertyName = "market_cap")]
        public string MarketCapUsd;
        [JsonProperty(PropertyName = "estimated_rewards")]
        public string EstimatedRewards;
        [JsonProperty(PropertyName = "estimated_rewards24")]
        public string EstimatedRewards24;
        [JsonProperty(PropertyName = "btc_revenue")]
        public string BtcRevenue;
        [JsonProperty(PropertyName = "btc_revenue24")]
        public string BtcRevenue24;
        [JsonProperty(PropertyName = "profitability")]
        public double Profitability;
        [JsonProperty(PropertyName = "profitability24")]
        public double Profitability24;
        [JsonProperty(PropertyName = "lagging")]
        public bool IsLagging;
        [JsonProperty(PropertyName = "timestamp")]
        public long TimeStamp;
    }

    class Program
    {
        const string JsonUrl = "http://whattomine.com/coins.json";

        static void Main(string[] args)
        {
            using (var client = new WebClient()) {
                var json = client.DownloadString(JsonUrl);
                var blob = JsonConvert.DeserializeObject<MineBlob>(json);
                // Do something with the data blob...
            }
        }
    }
}

【讨论】:

  • 谢谢,科比!这就是我正在寻找的,是的,我正在使用 NetonSoft JSON。所以要在字典值集合中收集所有硬币,只需挖掘字典值: var blob = JsonConvert.DeserializeObject(json).Values.ElementAt(0).Values;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 2012-07-04
相关资源
最近更新 更多