1

I've started learning C# and I've been going crazy because of this:

So I've read this of an online API to a string (ORIGINAL URL: https://api.coincap.io/v2/assets)

{"data":
[{"id":"bitcoin","rank":"1","symbol":"BTC","name":"Bitcoin","supply":"19231150.0000000000000000","maxSupply":"21000000.0000000000000000","marketCapUsd":"329260408122.7321099930951000","volumeUsd24Hr":"4938807092.4540332151393315","priceUsd":"17121.2022225780626740","changePercent24Hr":"-0.1557069639360563","vwap24Hr":"17167.6185642050963449","explorer":"https://blockchain.info/"},

{"id":"ethereum","rank":"2","symbol":"ETH","name":"Ethereum","supply":"122373866.2178000000000000","maxSupply":null,"marketCapUsd":"154858258278.7620692762684030","volumeUsd24Hr":"1556598691.7310630183480862","priceUsd":"1265.4520369826888988","changePercent24Hr":"-0.3774876415990869","vwap24Hr":"1271.6091894467065872","explorer":"https://etherscan.io/"},

{"id":"tether","rank":"3","symbol":"USDT","name":"Tether","supply":"65708194111.5796100000000000","maxSupply":null,"marketCapUsd":"65788824004.4908415146900527","volumeUsd24Hr":"6971097526.7488883793650250","priceUsd":"1.0012270903804526","changePercent24Hr":"0.0236756537376818","vwap24Hr":"1.0002996279301237","explorer":"https://www.omniexplorer.info/asset/31"}],

"timestamp":1670800790134}

And basically what I've been trying to do is to put the id's into a list but Haven't manage to parse the data. My code:

public class Coins
    {
        public Dictionary<string, ListDictionary> data { get; set; }
    }

    public class Program
    {

        static void Main(string[] args)
        { 
            List<string> coin_list = new List<string>();

            HttpClient Client = new HttpClient();

            string URL = "https://api.coincap.io/v2/";

            string response = Client.GetStringAsync(URL + "assets").Result; 
            
            Coins list = JsonConvert.DeserializeObject<Coins>(response);

            foreach(item in list.data)
            {
                 coin_list.add(item.id);
            }


            Console.ReadLine();
        }
    }

Can someone explain what am I doing wrong here?

Have spent the past couple of hours trying to figure this out

dbc
  • 104,963
  • 20
  • 228
  • 340
Vilenzo
  • 27
  • 4
  • You have to post ListDictionary class too – Serge Dec 12 '22 at 00:02
  • "_Can someone explain what am I doing wrong here?_" Quite probably, one or more of the your custom types involved as deserialization targets don't exactly match the respective json data structures. My big bet is that ListDictionay is such a mismatching type. Fix that ListDictionay class so that it (and its parameterized constructor, if it has one) **exactly** matches the respective json data structure from the json document, and Bob should be your uncle. (Also, make sure that the accessibility of the fields and property setters the data should be deserialized into is public.) –  Dec 12 '22 at 00:05
  • What @MySkullCaveIsADarkPlace@ said is more than likely correct. You can use a website like https://quicktype.io to create your object for you by pasting in raw JSON. – kasprdev Dec 12 '22 at 00:06

1 Answers1

1

if you need only ids, all your code you can put in one line

List<string> ids = JObject.Parse(response.Content)["data"]
                          .Select(d => (string)  d["id"]).ToList();

Console.WriteLine(string.Join(",",ids));

if you need more data you add properties you need to Coins class and show them to us, for example

List<Coin> coins = JObject.Parse(response.Content)["data"]
                          .Select(d =>  d.ToObject<Coin>()).ToList();

public class Coin
{
    public string id { get; set; }
    public string rank { get; set; }
    public string symbol { get; set; }
    public string name { get; set; }
    public string supply { get; set; }
    public string maxSupply { get; set; }
    public string marketCapUsd { get; set; }
    public string volumeUsd24Hr { get; set; }
    public string priceUsd { get; set; }
    public string changePercent24Hr { get; set; }
    public string vwap24Hr { get; set; }
    public string explorer { get; set; }
}

or if you want to use your code, fix the class Coins, you need a list for your json, not a dictionary

    public class Coins
    {
        public List<Coin> data { get; set; }
    }
Serge
  • 40,935
  • 4
  • 18
  • 45