skip to Main Content

I have an issue with deserializing the JSON file from the web to a List.

My code is down below, but it does not execute and shows Newtonsoft.Json.JsonSerializationException.

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

            callApi(coinDatas);
            Console.ReadKey();
        }

        private static async void callApi(List<Root> coinDatas)
        {
            string url = "https://api.coinstats.app/public/v1/coins?skip=0&limit=50&currency=USD";

            HttpClient httpClient = new HttpClient();

            var httpResponse = await httpClient.GetAsync(url);
            string jsonResponse = await httpResponse.Content.ReadAsStringAsync();

            coinDatas = JsonConvert.DeserializeObject<List<Root>>(jsonResponse);
        }
public class Coin
    {
        public string id { get; set; }
        public string icon { get; set; }
        public string name { get; set; }
        public string symbol { get; set; }
        public int rank { get; set; }
        public double price { get; set; }
        public double priceBtc { get; set; }
        public double volume { get; set; }
        public double marketCap { get; set; }
        public double availableSupply { get; set; }
        public double totalSupply { get; set; }
        public double priceChange1h { get; set; }
        public double priceChange1d { get; set; }
        public double priceChange1w { get; set; }
        public string contractAddress { get; set; }
        public int? decimals { get; set; }
    }

    public class Root
    {
        public List<Coin> coins { get; set; }
    }

2

Answers


  1. I’ve donloaded JSON from your url. It looks like this:

    {
        "coins": [
            {
                "id": "bitcoin",
                "icon": "https://static.coinstats.app/coins/1650455588819.png",
                "name": "Bitcoin",
                "symbol": "BTC",
                "rank": 1,
                "price": 16591.950104477022,
                "priceBtc": 1,
                "volume": 9476109498.476653,
                "marketCap": 319384453847.01608,
                "availableSupply": 19249362,
                "totalSupply": 21000000,
                "priceChange1h": 0.15,
                "priceChange1d": 0.05,
                "priceChange1w": -1.21,
                "websiteUrl": "http://www.bitcoin.org",
                "twitterUrl": "https://twitter.com/bitcoin",
                "exp": [
                    "https://blockchair.com/bitcoin/",
                    "https://btc.com/",
                    "https://btc.tokenview.io/"
                ]
            }
        ]
    }
    

    I suppose ytour problem in coinDatas = JsonConvert.DeserializeObject<List<Root>>(jsonResponse); line.

    You have only one root element, lit a list of them.

    And, by the way, your code does not return any value.

    I recommend you to rwerite your method to somewthing like this:

    private static async Task<Root> callApiAsync()
    {
        string url = "https://api.coinstats.app/public/v1/coins?skip=0&limit=50&currency=USD";
    
        HttpClient httpClient = new HttpClient();
    
        var httpResponse = await httpClient.GetAsync(url);
        string jsonResponse = await httpResponse.Content.ReadAsStringAsync();
    
        return JsonConvert.DeserializeObject<Root>(jsonResponse);
    }
    

    then you can use this method in Main (notice async word in this method too):

    static async void Main(string[] args)
    {
        var coinDatas = await callApiAsync();
        Console.ReadKey();
    }
    
    Login or Signup to reply.
  2. if you just need a list of coins, you can change the code to this

    async Task Main()
    {
    
        List<Coin> coins = await callApiAsync();
        Console.ReadLine();
    }
    
    static async Task<List<Coin>> callApiAsync()
    {
        string url = "https://api.coinstats.app/public/v1/coins?skip=0&limit=50&currency=USD";
    
        // your another code
    
        var data= JsonConvert.DeserializeObject<Root>(jsonResponse);
        return data.coins;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search