8

I want to get all variables from https://api.coinmarketcap.com/v1/ticker/ in my c# console application. How can I do this?

I started with getting the whole page as a stream. What to do now?

private static void start_get()
{
    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create
        (string.Format("https://api.coinmarketcap.com/v1/ticker/"));

    WebReq.Method = "GET";

    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

    Console.WriteLine(WebResp.StatusCode);
    Console.WriteLine(WebResp.Server);

    Stream Answer = WebResp.GetResponseStream();
    StreamReader _Answer = new StreamReader(Answer);
    Console.WriteLine(_Answer.ReadToEnd());
}
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
D. Jung
  • 145
  • 1
  • 1
  • 9
  • What do you want, a string containing information or an object representation of that json result? – ColinM Jun 27 '17 at 08:39
  • check out: https://stackoverflow.com/questions/8270464/best-way-to-call-a-json-webservice-from-a-net-console there are a couple of answers that should get you up and running – MattjeS Jun 27 '17 at 08:39
  • Possible duplicate of [Best way to call a JSON WebService from a .NET Console](https://stackoverflow.com/questions/8270464/best-way-to-call-a-json-webservice-from-a-net-console) – Nasreddine Jun 27 '17 at 08:41

2 Answers2

15

First you need a custom class to use for deserialization:

public class Item
{
    public string id { get; set; }
    public string name { get; set; }
    public string symbol { get; set; }
    public string rank { get; set; }
    public string price_usd { get; set; }
    [JsonProperty(PropertyName = "24h_volume_usd")]   //since in c# variable names cannot begin with a number, you will need to use an alternate name to deserialize
    public string volume_usd_24h { get; set; }
    public string market_cap_usd { get; set; }
    public string available_supply { get; set; }
    public string total_supply { get; set; }
    public string percent_change_1h { get; set; }
    public string percent_change_24h { get; set; }
    public string percent_change_7d { get; set; }
    public string last_updated { get; set; }
}

Next, you can use Newtonsoft Json, a free JSON serialization and deserialization framework in the following way to get your items (include the following using statements):

using System.Net;
using System.IO;
using Newtonsoft.Json;

private static void start_get()
{
    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://api.coinmarketcap.com/v1/ticker/"));

    WebReq.Method = "GET";

    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

    Console.WriteLine(WebResp.StatusCode);
    Console.WriteLine(WebResp.Server);

    string jsonString;
    using (Stream stream = WebResp.GetResponseStream())   //modified from your code since the using statement disposes the stream automatically when done
    {
       StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
       jsonString = reader.ReadToEnd();
    }

    List<Item> items = JsonConvert.DeserializeObject<List<Item>>(jsonString);

    Console.WriteLine(items.Count);     //returns 921, the number of items on that page
}

Finally, the list of elements is stored in items.

Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
3

A simplified version of Keyur PATEL's work.

static void GetCoinValues()
{
    string json = new WebClient().DownloadString("https://api.coinmarketcap.com/v1/ticker/");

    List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);

    foreach (var item in items)
    {
        Console.WriteLine("ID: " + item.id.ToUpper());
        Console.WriteLine("Name: " + item.name.ToUpper());
        Console.WriteLine("Symbol: " + item.symbol.ToUpper());
        Console.WriteLine("Rank: " + item.rank.ToUpper());
        Console.WriteLine("Price (USD): " + item.price_usd.ToUpper());
        Console.WriteLine("\n");
    }
}
Draken
  • 3,134
  • 13
  • 34
  • 54
agleno
  • 388
  • 4
  • 17
  • How would you get the data for a specific symbol using your method? I added IF (item.symbol = mySymbol) { set some variables} in the loop but it doesnt return anything. – goodfella Jan 06 '18 at 17:14
  • 1
    Nevermind I got it figured out...had a problem with case matching so I had to uppercase my string. However the json request seems to limit the list. Not giving the full list of currencies. Anyway to solve around this? – goodfella Jan 07 '18 at 16:17
  • can i persuade you to shar eyour github repo of this? – aaron lilly Oct 08 '19 at 20:32
  • Note that `WebClient` implements `IDisposable` and should have `Using` or is otherwise disposed. – Mmm Nov 03 '21 at 19:11