2

I am making some application with cryptocurrencies and I have a problem with API I have found somewhere.

I need some tips how to simplify my class for serialization.

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

I have used http://json2csharp.com/ class generator - but after that I was left with hundreds of classes that looked the same - only had other names. I have tried replacing it but always was left with nulls.

Now I am with that:-

    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));
    }

Or maybe should I use different deserializer? Or just check for an object every time iterating for loop?

dbc
  • 104,963
  • 20
  • 228
  • 340
Bubiec
  • 363
  • 2
  • 10
  • 2
    Use a dictionary. In your `RootObject` add a member `public Dictionary Data`, then create a `Data` type for the fixed properties of each cryptocurrency. See [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/q/34213566/3744182) for details. – dbc Jul 24 '17 at 20:42

3 Answers3

2

I make a little test using RestSharp and it works well

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; }

    }
}

Basically I change the Data property for a Dictonary type. That way it will serialize all Data inside the Dictionary and you can use like that

data.Data["SUB"].Id
Milton Filho
  • 525
  • 3
  • 17
1

What you've got there is a dictionary. Try these classes instead.

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; }
}
Craig W.
  • 17,838
  • 6
  • 49
  • 82
-1

Try to deserializer with newtonsoft

enter link description here

  • Welcome to stackoverflow. Thanks for posting this, but it does not answer the question. OP is already using newtonsoft, and is wondering how their data model should be designed to easily deserialize the sample JSON with it. Further, it is basically a link-only answer, which is [discouraged](https://meta.stackexchange.com/a/8259/344280). Take a look at [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) which explains *What, specifically, is the question asking for? Make sure your answer provides that* and *Brevity is acceptable, but fuller explanations are better.* – dbc Jul 25 '17 at 00:24