2017-06-27 6 views
3

https://api.coinmarketcap.com/v1/ticker/からすべての変数を私のC#コンソールアプリケーションに入れたいです。 どうすればいいですか?httpwebrequestを使ってC#でjson apiからデータを取得するには?

まず、ページ全体をストリームとして取得することから始めました。今何をする?

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()); 
} 
+0

あなたは、情報またはそのJSON結果のオブジェクト表現を含む文字列を何をしたいですか? – ColinM

+0

チェックアウト:https://stackoverflow.com/questions/8270464/best-way-to-call-a-json-webservice-from-a-net-consoleそこには、いくつかの回答があります。 – MattjeS

+0

[.NETコンソールからJSON WebServiceを呼び出すための最良の方法](https://stackoverflow.com/questions/8270464/best-way-to-call-a-json-webservice-from-a-net) -console) – Nasreddine

答えて

5

まずあなたは、直列化復元に使用するカスタムクラスを必要とする:

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

次に、あなたはあなたのアイテムを取得するには、次のようにNewtonsoft Json、無料のJSONのシリアライズとデシリアライズフレームワークを使用することができます(インクルードusing文)以下:

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 
} 

最後に、要素のリストがitemsに格納されます。

0

Keyur PATELの簡単なバージョンです。

静的な無効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"); 
     } 
    } 




} 
+0

メソッドを使用して特定のシンボルのデータを取得するにはどうすればよいですか? IF(item.symbol = mySymbol){ループにいくつかの変数を設定}を追加しましたが、何も返しません。 – goodfella

+0

Nevermindわかったのですが、大文字と小文字のマッチングに問題があり、文字列を大文字にする必要がありました。しかし、jsonの要求はリストを制限するようです。通貨の完全なリストを与えていない。とにかくこれを解決するには? – goodfella

関連する問題