C# 要列出的对象是什么

C# 要列出的对象是什么,c#,arrays,json,object,deserialization,C#,Arrays,Json,Object,Deserialization,我正试图通过在我的宠物C#项目中使用JSON来获得前5名最赚钱的采矿硬币 问题是该站点只返回单个对象而不是数组(为了简洁起见,我对属性列表进行了排序): 我真的不需要硬币的名字,因为标签就足够了,所以我想要这样的东西: [ { "id":168, "tag":"HUSH", "algorithm":"Equihash", }, { "id":167, "tag":"ZCL", "algorithm":"Eq

我正试图通过在我的宠物C#项目中使用JSON来获得前5名最赚钱的采矿硬币

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

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

[
   {
      "id":168,
      "tag":"HUSH",
      "algorithm":"Equihash",
   },
   {
      "id":167,
      "tag":"ZCL",
      "algorithm":"Equihash"
   }
]
我尝试使用,但它生成了一系列具有相同属性的类,每个硬币一个。而且,由于不断添加新代码,我不想每次都更改代码


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

除非您有一个非常具体的用例,否则我建议将其用作事实上的JSON库。这会给你省去很多麻烦

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

将对象映射到
词典

如果可能,首选自动序列化/反序列化:

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...
            }
        }
    }
}
使用System.Collections.Generic;
Net系统;
使用Newtonsoft.Json;
名称空间WhatToMine
{
使用MineBlob=字典;
类CoinBlob
{
[JsonProperty(PropertyName=“id”)]
公共int Id;
[JsonProperty(PropertyName=“tag”)]
公共字符串标签;
[JsonProperty(PropertyName=“algorithm”)]
公共字符串算法;
[JsonProperty(PropertyName=“block\u time”)]
公共双阻塞时间;
[JsonProperty(PropertyName=“block_奖励”)]
公共双倍奖励;
[JsonProperty(PropertyName=“block\u reward24”)]
公开双封杀24;
[JsonProperty(PropertyName=“last_block”)]
公共长廊;
[JsonProperty(PropertyName=“困难”)]
公众双重困难;
[JsonProperty(PropertyName=“困难24”)]
公共双重困难24;
[JsonProperty(PropertyName=“nethash”)]
公共长NetHash;
[JsonProperty(PropertyName=“汇率”)]
公共双汇率;
[JsonProperty(PropertyName=“exchange_rate24”)]
公共双交换24;
[JsonProperty(PropertyName=“汇率”\u vol”)]
公共双交换量;
[JsonProperty(PropertyName=“汇率”)]
公共字符串交换货币;
[JsonProperty(PropertyName=“市值”)]
公共部门:美元;
[JsonProperty(PropertyName=“估计的奖励”)]
向上估计公共字符串;
[JsonProperty(PropertyName=“估计的报酬24”)]
向上估计的公共字符串24;
[JsonProperty(PropertyName=“btc_收入”)]
公共场所;
[JsonProperty(PropertyName=“btc_revenue24”)]
公共字符串BtcRevenue24;
[JsonProperty(PropertyName=“盈利能力”)]
公共双盈利;
[JsonProperty(PropertyName=“profitability24”)]
公共双盈利24;
[JsonProperty(PropertyName=“laging”)]
公共图书馆;
[JsonProperty(PropertyName=“timestamp”)]
公共长时间戳;
}
班级计划
{
常量字符串JsonUrl=”http://whattomine.com/coins.json";
静态void Main(字符串[]参数)
{
使用(var client=new WebClient()){
var json=client.DownloadString(JsonUrl);
var blob=JsonConvert.DeserializeObject(json);
//对数据块执行某些操作。。。
}
}
}
}

除非您有一个非常具体的用例,否则我建议您将其用作事实上的JSON库。这会给你省去很多麻烦

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

将对象映射到
词典

如果可能,首选自动序列化/反序列化:

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...
            }
        }
    }
}
使用System.Collections.Generic;
Net系统;
使用Newtonsoft.Json;
名称空间WhatToMine
{
使用MineBlob=字典;
类CoinBlob
{
[JsonProperty(PropertyName=“id”)]
公共int Id;
[JsonProperty(PropertyName=“tag”)]
公共字符串标签;
[JsonProperty(PropertyName=“algorithm”)]
公共字符串算法;
[JsonProperty(PropertyName=“block\u time”)]
公共双阻塞时间;
[JsonProperty(PropertyName=“block_奖励”)]
公共双倍奖励;
[JsonProperty(PropertyName=“block\u reward24”)]
公开双封杀24;
[JsonProperty(PropertyName=“last_block”)]
公共长廊;
[JsonProperty(PropertyName=“困难”)]
公众双重困难;
[JsonProperty(PropertyName=“困难24”)]
公共双重困难24;
[JsonProperty(PropertyName=“nethash”)]
公共长NetHash;
[JsonProperty(PropertyName=“汇率”)]
公共双汇率;
[JsonProperty(PropertyName=“exchange_rate24”)]
公共双交换24;
[JsonProperty(PropertyName=“汇率”\u vol”)]
公共双交换量;
[JsonProperty(PropertyName=“汇率”)]
公共字符串交换货币;
[JsonProperty(PropertyName=“市值”)]
公共部门:美元;
[JsonProperty(PropertyName=“估计的奖励”)]
向上估计公共字符串;
[JsonProperty(PropertyName=“估计的报酬24”)]
向上估计的公共字符串24;
[JsonProperty(PropertyName=“btc_收入”)]
公共场所;
[JsonProperty(PropertyName=“btc_revenue24”)]
公共字符串BtcRevenue24;
[JsonProperty(PropertyName=“盈利能力”)]