简化 JSON 个要反序列化的对象

Simplify JSON object to deserialize

我正在使用加密货币制作一些应用程序,但我遇到了 API 我在某处找到的问题。

我需要一些技巧来简化我的 class 序列化。

public class Data
{
    public SUB SUB { get; set; }
    public USC USC { get; set; }
    public DUX DUX { get; set; }
    public XPS XPS { get; set; }
    public EQT EQT { get; set; }
    ... //and a lot more of same classes

}

Here is that REST page with JSON

我使用了 http://json2csharp.com/ class 生成器 - 但在那之后我只剩下数百个看起来相同的 classes - 只是有不同的名字。我试过替换它,但总是留下空值。

现在我同意了:-

    public class Data
    {
        public string Id { get; set; }
        public string Url { get; set; }
        public string ImageUrl { get; set; }
        public string Name { get; set; }
    ...
    }
    public class RootObject
    {
        public string BaseLinkUrl { get; set; }
        public List<List<Data>> Data { get; set; }
        public int Type { get; set; }
    }


    public static async Task<T> DeserializeStringToObject<T>(string url)
    {
        return JsonConvert
            .DeserializeObject<T>(await GetStreamFromUr(url));
    }

或者也许我应该使用不同的解串器?还是每次迭代for循环都检查一个对象?

我使用 RestSharp 进行了一些测试,效果很好

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new RestClient("https://www.cryptocompare.com/api");

            var response = client.Execute<DataContainer>(new RestRequest("/data/coinlist"));

            var data = response.Data;        }
    }

    public class DataContainer
    {
        public string Message { get; set; }
        public Dictionary<string, DataItem> Data { get; set; }
    }

    public class DataItem
    {
        public string Id { get; set; }
        public string Url { get; set; }
        public string ImageUrl { get; set; }
        public string Name { get; set; }

    }
}

基本上我将数据 属性 更改为字典类型。这样它将序列化字典中的所有数据,你可以像那样使用

data.Data["SUB"].Id

你得到的是一本字典。试试这些 类。

public class RootObject
{
    public string Response { get; set; }
    public string Message { get; set; }
    public string BaseImageUrl { get; set; }
    public string BaseLinkUrl { get; set; }
    public Dictionary<string, CurrencyDefinition> Data { get; set; }
    public int Type { get; set; }
}

public class CurrencyDefinition
{
    public string Id { get; set; }
    public string Url { get; set; }
    public string ImageUrl { get; set; }
    public string Name { get; set; }
    public string CoinName { get; set; }
    public string FullName { get; set; }
    public string Algorithm { get; set; }
    public string ProofType { get; set; }
    public string FullyPremined { get; set; }
    public string TotalCoinSupply { get; set; }
    public string PreMinedValue { get; set; }
    public string TotalCoinsFreeFloat { get; set; }
    public string SortOrder { get; set; }
}

尝试使用 newtonsoft 进行反序列化

enter link description here