回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,62 +1,62 @@
|
|
1
|
-
jsonの外側の部分("quotes")も作れば、`JsonConvert.DeserializeObject`一発でデシリアライズできます。
|
2
|
-
|
3
|
-
Listと個々のPairがごっちゃになっているようです。
|
4
|
-
ローカル変数(var pair1)はメソッドを抜けたら消えてしまいます。
|
5
|
-
|
6
|
-
```
|
7
|
-
using System;
|
8
|
-
using System.Collections.Generic;
|
9
|
-
using System.Linq;
|
10
|
-
using System.Windows.Forms;
|
11
|
-
using Newtonsoft.Json;
|
12
|
-
|
13
|
-
namespace Questions237472
|
14
|
-
{
|
15
|
-
public partial class Form1 : Form
|
16
|
-
{
|
17
|
-
private List<Pair> pairs;
|
18
|
-
|
19
|
-
public Form1()
|
20
|
-
{
|
21
|
-
InitializeComponent();
|
22
|
-
button1.Text = "USD/JPY";
|
23
|
-
}
|
24
|
-
|
25
|
-
private void Form1_Load(object sender, EventArgs e)
|
26
|
-
{
|
27
|
-
var fileUrl = @"https://www.gaitameonline.com/rateaj/getrate";
|
28
|
-
var root = JsonConvert.DeserializeObject<Root>(DownloadString(fileUrl));
|
29
|
-
pairs = root.quotes;
|
30
|
-
}
|
31
|
-
|
32
|
-
private void button1_Click(object sender, EventArgs e)
|
33
|
-
{
|
34
|
-
var pair1 = pairs.First(x => x.currencyPairCode == "USDJPY");
|
35
|
-
textBox1.AppendText("高値は" + pair1.high + "です");
|
36
|
-
}
|
37
|
-
|
38
|
-
private string DownloadString(string url)
|
39
|
-
{
|
40
|
-
using(var webClient = new System.Net.WebClient())
|
41
|
-
{
|
42
|
-
return webClient.DownloadString(url);
|
43
|
-
}
|
44
|
-
}
|
45
|
-
|
46
|
-
private class Root
|
47
|
-
{
|
48
|
-
public List<Pair> quotes { get; set; }
|
49
|
-
}
|
50
|
-
|
51
|
-
private class Pair
|
52
|
-
{
|
53
|
-
public double high { get; set; }
|
54
|
-
public double open { get; set; }
|
55
|
-
public double bid { get; set; }
|
56
|
-
public string currencyPairCode { get; set; }
|
57
|
-
public double ask { get; set; }
|
58
|
-
public double low { get; set; }
|
59
|
-
}
|
60
|
-
}
|
61
|
-
}
|
1
|
+
jsonの外側の部分(`"quotes"`)も作れば、`JsonConvert.DeserializeObject`一発でデシリアライズできます。
|
2
|
+
|
3
|
+
`List`と個々の`Pair`がごっちゃになっているようです。
|
4
|
+
ローカル変数(`var pair1`)はメソッドを抜けたら消えてしまいます。
|
5
|
+
|
6
|
+
```cs
|
7
|
+
using System;
|
8
|
+
using System.Collections.Generic;
|
9
|
+
using System.Linq;
|
10
|
+
using System.Windows.Forms;
|
11
|
+
using Newtonsoft.Json;
|
12
|
+
|
13
|
+
namespace Questions237472
|
14
|
+
{
|
15
|
+
public partial class Form1 : Form
|
16
|
+
{
|
17
|
+
private List<Pair> pairs;
|
18
|
+
|
19
|
+
public Form1()
|
20
|
+
{
|
21
|
+
InitializeComponent();
|
22
|
+
button1.Text = "USD/JPY";
|
23
|
+
}
|
24
|
+
|
25
|
+
private void Form1_Load(object sender, EventArgs e)
|
26
|
+
{
|
27
|
+
var fileUrl = @"https://www.gaitameonline.com/rateaj/getrate";
|
28
|
+
var root = JsonConvert.DeserializeObject<Root>(DownloadString(fileUrl));
|
29
|
+
pairs = root.quotes;
|
30
|
+
}
|
31
|
+
|
32
|
+
private void button1_Click(object sender, EventArgs e)
|
33
|
+
{
|
34
|
+
var pair1 = pairs.First(x => x.currencyPairCode == "USDJPY");
|
35
|
+
textBox1.AppendText("高値は" + pair1.high + "です");
|
36
|
+
}
|
37
|
+
|
38
|
+
private string DownloadString(string url)
|
39
|
+
{
|
40
|
+
using(var webClient = new System.Net.WebClient())
|
41
|
+
{
|
42
|
+
return webClient.DownloadString(url);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
private class Root
|
47
|
+
{
|
48
|
+
public List<Pair> quotes { get; set; }
|
49
|
+
}
|
50
|
+
|
51
|
+
private class Pair
|
52
|
+
{
|
53
|
+
public double high { get; set; }
|
54
|
+
public double open { get; set; }
|
55
|
+
public double bid { get; set; }
|
56
|
+
public string currencyPairCode { get; set; }
|
57
|
+
public double ask { get; set; }
|
58
|
+
public double low { get; set; }
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
62
|
```
|