.NET/Web APIからJSONを取得し値を表示する・DataContractJsonSerializer
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
* Web APIからJSONを取得し値を表示する・DataContractJsonSe...
.NET標準の''DataContractJsonSerializer''を使用して、bitfl...
取得したJSONをデシリアライズし画面に表示するまでのサンプ...
''本サンプルコードでアクセスするbitflyerのTicker Web API''
-[[https://lightning.bitflyer.com/docs?lang=ja#ticker]]
サンプルコードは C#, Visual Basic(VB)を公開しています。~
尚、HTTPによるアクセスは、以下リンクの記事のサンプルコー...
-[[HttpClientを使って指定したURLのページを取得する>.NET/H...
#htmlinsert(windev-top.html)
#contents
* 関連サイト [#oe3a9a14]
-[[Microsoft .NET DataContractJsonSerializer クラス>https...
* 関連記事 [#m923e443]
-[[HttpClientを使って指定したURLのページを取得する>.NET/H...
* 動作環境 [#y668fb01]
-Windows 10 ver.1909
-Visual Studio 2019
-.NET Core 3.1
-コンソールアプリケーション
* サンプルコード [#m4e2257d]
以下にC#,Visual Basic(VB)のサンプルコードを紹介します。~
また、実行時のキャプチャもあります。
** C# [#d7559a6c]
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
class Program
{
static void Main(string[] args)
{
string endpoint = @"https://api.bitflyer.com/v1/";
string tickerApi = @"ticker?product_code=";
string product_code = @"BTC_JPY";
Program app = new Program();
HttpClient client = new HttpClient();
string responseJson = (app.HttpContent(endpoint ...
Console.WriteLine("== TICKER BTC_JPY TICKER ==\n...
DataContractJsonSerializerSettings settings = ne...
{
DateTimeFormat = new System.Runtime.Serializ...
};
Ticker deserialized;
DataContractJsonSerializer js = new DataContract...
using (MemoryStream ms = new MemoryStream(Encodi...
{
deserialized = js.ReadObject(ms) as Ticker;
}
Console.WriteLine("== DESERIALIZED TICKER ==");
app.ShowTicker(deserialized);
}
[System.Runtime.Serialization.DataContract]
public class Ticker
{
[System.Runtime.Serialization.DataMember(Name = ...
public string product_code { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public DateTime timestamp { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public int tick_id { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal best_bid { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal best_ask { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal best_bid_size { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal total_bid_depth { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal total_ask_depth { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal ltp { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal volume { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal volume_by_product { get; set; }
}
private void ShowTicker(Ticker t)
{
Console.WriteLine("product_code :{0}", t.pro...
Console.WriteLine("timestamp :{0}", t.tim...
Console.WriteLine("tick_id :{0}", t.tic...
Console.WriteLine("best_bid :{0}", t.bes...
Console.WriteLine("best_ask :{0}", t.bes...
Console.WriteLine("best_bid_size :{0}", t.bes...
Console.WriteLine("total_bid_depth :{0}", t.tot...
Console.WriteLine("total_ask_depth :{0}", t.tot...
Console.WriteLine("ltp :{0}", t.ltp);
Console.WriteLine("volume :{0}", t.vol...
Console.WriteLine("volume_by_product:{0}", t.vol...
}
private async Task<string> HttpContent(string url, H...
{
HttpResponseMessage response = null;
try
{
response = await clinet.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content...
return await response.Content.ReadAsStringAs...
}
catch (Exception ex)
{
Console.WriteLine("Exception Message :{0} ",...
}
return null;
}
}
** Visual Basic [#b2173783]
Imports System.IO
Imports System.Text
Imports System.Net.Http
Imports System.Runtime.Serialization.Json
Module Program
Sub Main(args As String())
Dim endpoint As String = "https://api.bitflyer.c...
Dim tickerApi As String = "ticker?product_code="
Dim product_code As String = "BTC_JPY"
Dim client As HttpClient = New HttpClient()
Dim responseJson As String = (HttpContent(endpoi...
Console.WriteLine("== TICKER BTC_JPY TICKER ==" ...
Dim settings As DataContractJsonSerializerSettin...
.DateTimeFormat = New System.Runtime.Seriali...
}
Dim deserialized As Ticker
Dim js As DataContractJsonSerializer = New DataC...
Using ms = New MemoryStream(Encoding.UTF8.GetByt...
deserialized = TryCast(js.ReadObject(ms), Ti...
End Using
Console.WriteLine("== DESERIALIZED TICKER ==")
ShowTicker(deserialized)
End Sub
<System.Runtime.Serialization.DataContract>
Public Class Ticker
<System.Runtime.Serialization.DataMember(Name:="...
Public Property product_code As String
<System.Runtime.Serialization.DataMember(Name:="...
Public Property timestamp As DateTime
<System.Runtime.Serialization.DataMember(Name:="...
Public Property tick_id As Integer
<System.Runtime.Serialization.DataMember(Name:="...
Public Property best_bid As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property best_ask As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property best_bid_size As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property total_bid_depth As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property total_ask_depth As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property ltp As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property volume As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property volume_by_product As Decimal
End Class
Private Sub ShowTicker(ByVal t As Ticker)
Console.WriteLine("product_code :{0}", t.pro...
Console.WriteLine("timestamp :{0}", t.tim...
Console.WriteLine("tick_id :{0}", t.tic...
Console.WriteLine("best_bid :{0}", t.bes...
Console.WriteLine("best_ask :{0}", t.bes...
Console.WriteLine("best_bid_size :{0}", t.bes...
Console.WriteLine("total_bid_depth :{0}", t.tot...
Console.WriteLine("total_ask_depth :{0}", t.tot...
Console.WriteLine("ltp :{0}", t.ltp)
Console.WriteLine("volume :{0}", t.vol...
Console.WriteLine("volume_by_product:{0}", t.vol...
End Sub
Private Async Function HttpContent(ByVal url As Stri...
Dim response As HttpResponseMessage = Nothing
Try
response = Await clinet.GetAsync(url)
response.EnsureSuccessStatusCode()
Dim responseBody As String = Await response....
Return Await response.Content.ReadAsStringAs...
Catch ex As Exception
Console.WriteLine("Exception Message :{0} ",...
End Try
Return Nothing
End Function
End Module
** 実行結果 [#i069c584]
上記サンプルコードの実行結果になります。
#ref(01.png)
以上、.NET標準の DataContractJsonSerializer を使ってJSON...
#htmlinsert(windev-btm.html)
終了行:
#navi(../)
* Web APIからJSONを取得し値を表示する・DataContractJsonSe...
.NET標準の''DataContractJsonSerializer''を使用して、bitfl...
取得したJSONをデシリアライズし画面に表示するまでのサンプ...
''本サンプルコードでアクセスするbitflyerのTicker Web API''
-[[https://lightning.bitflyer.com/docs?lang=ja#ticker]]
サンプルコードは C#, Visual Basic(VB)を公開しています。~
尚、HTTPによるアクセスは、以下リンクの記事のサンプルコー...
-[[HttpClientを使って指定したURLのページを取得する>.NET/H...
#htmlinsert(windev-top.html)
#contents
* 関連サイト [#oe3a9a14]
-[[Microsoft .NET DataContractJsonSerializer クラス>https...
* 関連記事 [#m923e443]
-[[HttpClientを使って指定したURLのページを取得する>.NET/H...
* 動作環境 [#y668fb01]
-Windows 10 ver.1909
-Visual Studio 2019
-.NET Core 3.1
-コンソールアプリケーション
* サンプルコード [#m4e2257d]
以下にC#,Visual Basic(VB)のサンプルコードを紹介します。~
また、実行時のキャプチャもあります。
** C# [#d7559a6c]
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
class Program
{
static void Main(string[] args)
{
string endpoint = @"https://api.bitflyer.com/v1/";
string tickerApi = @"ticker?product_code=";
string product_code = @"BTC_JPY";
Program app = new Program();
HttpClient client = new HttpClient();
string responseJson = (app.HttpContent(endpoint ...
Console.WriteLine("== TICKER BTC_JPY TICKER ==\n...
DataContractJsonSerializerSettings settings = ne...
{
DateTimeFormat = new System.Runtime.Serializ...
};
Ticker deserialized;
DataContractJsonSerializer js = new DataContract...
using (MemoryStream ms = new MemoryStream(Encodi...
{
deserialized = js.ReadObject(ms) as Ticker;
}
Console.WriteLine("== DESERIALIZED TICKER ==");
app.ShowTicker(deserialized);
}
[System.Runtime.Serialization.DataContract]
public class Ticker
{
[System.Runtime.Serialization.DataMember(Name = ...
public string product_code { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public DateTime timestamp { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public int tick_id { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal best_bid { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal best_ask { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal best_bid_size { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal total_bid_depth { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal total_ask_depth { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal ltp { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal volume { get; set; }
[System.Runtime.Serialization.DataMember(Name = ...
public Decimal volume_by_product { get; set; }
}
private void ShowTicker(Ticker t)
{
Console.WriteLine("product_code :{0}", t.pro...
Console.WriteLine("timestamp :{0}", t.tim...
Console.WriteLine("tick_id :{0}", t.tic...
Console.WriteLine("best_bid :{0}", t.bes...
Console.WriteLine("best_ask :{0}", t.bes...
Console.WriteLine("best_bid_size :{0}", t.bes...
Console.WriteLine("total_bid_depth :{0}", t.tot...
Console.WriteLine("total_ask_depth :{0}", t.tot...
Console.WriteLine("ltp :{0}", t.ltp);
Console.WriteLine("volume :{0}", t.vol...
Console.WriteLine("volume_by_product:{0}", t.vol...
}
private async Task<string> HttpContent(string url, H...
{
HttpResponseMessage response = null;
try
{
response = await clinet.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content...
return await response.Content.ReadAsStringAs...
}
catch (Exception ex)
{
Console.WriteLine("Exception Message :{0} ",...
}
return null;
}
}
** Visual Basic [#b2173783]
Imports System.IO
Imports System.Text
Imports System.Net.Http
Imports System.Runtime.Serialization.Json
Module Program
Sub Main(args As String())
Dim endpoint As String = "https://api.bitflyer.c...
Dim tickerApi As String = "ticker?product_code="
Dim product_code As String = "BTC_JPY"
Dim client As HttpClient = New HttpClient()
Dim responseJson As String = (HttpContent(endpoi...
Console.WriteLine("== TICKER BTC_JPY TICKER ==" ...
Dim settings As DataContractJsonSerializerSettin...
.DateTimeFormat = New System.Runtime.Seriali...
}
Dim deserialized As Ticker
Dim js As DataContractJsonSerializer = New DataC...
Using ms = New MemoryStream(Encoding.UTF8.GetByt...
deserialized = TryCast(js.ReadObject(ms), Ti...
End Using
Console.WriteLine("== DESERIALIZED TICKER ==")
ShowTicker(deserialized)
End Sub
<System.Runtime.Serialization.DataContract>
Public Class Ticker
<System.Runtime.Serialization.DataMember(Name:="...
Public Property product_code As String
<System.Runtime.Serialization.DataMember(Name:="...
Public Property timestamp As DateTime
<System.Runtime.Serialization.DataMember(Name:="...
Public Property tick_id As Integer
<System.Runtime.Serialization.DataMember(Name:="...
Public Property best_bid As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property best_ask As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property best_bid_size As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property total_bid_depth As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property total_ask_depth As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property ltp As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property volume As Decimal
<System.Runtime.Serialization.DataMember(Name:="...
Public Property volume_by_product As Decimal
End Class
Private Sub ShowTicker(ByVal t As Ticker)
Console.WriteLine("product_code :{0}", t.pro...
Console.WriteLine("timestamp :{0}", t.tim...
Console.WriteLine("tick_id :{0}", t.tic...
Console.WriteLine("best_bid :{0}", t.bes...
Console.WriteLine("best_ask :{0}", t.bes...
Console.WriteLine("best_bid_size :{0}", t.bes...
Console.WriteLine("total_bid_depth :{0}", t.tot...
Console.WriteLine("total_ask_depth :{0}", t.tot...
Console.WriteLine("ltp :{0}", t.ltp)
Console.WriteLine("volume :{0}", t.vol...
Console.WriteLine("volume_by_product:{0}", t.vol...
End Sub
Private Async Function HttpContent(ByVal url As Stri...
Dim response As HttpResponseMessage = Nothing
Try
response = Await clinet.GetAsync(url)
response.EnsureSuccessStatusCode()
Dim responseBody As String = Await response....
Return Await response.Content.ReadAsStringAs...
Catch ex As Exception
Console.WriteLine("Exception Message :{0} ",...
End Try
Return Nothing
End Function
End Module
** 実行結果 [#i069c584]
上記サンプルコードの実行結果になります。
#ref(01.png)
以上、.NET標準の DataContractJsonSerializer を使ってJSON...
#htmlinsert(windev-btm.html)
ページ名: