回答編集履歴

1

見直しキャンペーン中

2023/07/18 21:49

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,123 +1,62 @@
1
- jsonの外側の部分("quotes")も作れば、`JsonConvert.DeserializeObject`一発でデシリアライズできます。
1
+ jsonの外側の部分(`"quotes"`)も作れば、`JsonConvert.DeserializeObject`一発でデシリアライズできます。
2
2
 
3
+ `List`と個々の`Pair`がごっちゃになっているようです。
4
+ ローカル変数(`var pair1`)はメソッドを抜けたら消えてしまいます。
3
5
 
4
-
5
- Listと個々のPairがごっちゃになっているようです。
6
-
7
- ローカル変数(var pair1)はメソッドを抜けたら消えてしまいます。
8
-
9
-
10
-
11
- ```C#
6
+ ```cs
12
-
13
7
  using System;
14
-
15
8
  using System.Collections.Generic;
16
-
17
9
  using System.Linq;
18
-
19
10
  using System.Windows.Forms;
20
-
21
11
  using Newtonsoft.Json;
22
12
 
23
-
24
-
25
13
  namespace Questions237472
26
-
27
14
  {
28
-
29
15
  public partial class Form1 : Form
30
-
31
16
  {
32
-
33
17
  private List<Pair> pairs;
34
18
 
35
-
36
-
37
19
  public Form1()
38
-
39
20
  {
40
-
41
21
  InitializeComponent();
42
-
43
22
  button1.Text = "USD/JPY";
44
-
45
23
  }
46
24
 
47
-
48
-
49
25
  private void Form1_Load(object sender, EventArgs e)
50
-
51
26
  {
52
-
53
27
  var fileUrl = @"https://www.gaitameonline.com/rateaj/getrate";
54
-
55
28
  var root = JsonConvert.DeserializeObject<Root>(DownloadString(fileUrl));
56
-
57
29
  pairs = root.quotes;
58
-
59
30
  }
60
31
 
61
-
62
-
63
32
  private void button1_Click(object sender, EventArgs e)
64
-
65
33
  {
66
-
67
34
  var pair1 = pairs.First(x => x.currencyPairCode == "USDJPY");
68
-
69
35
  textBox1.AppendText("高値は" + pair1.high + "です");
70
-
71
36
  }
72
37
 
73
-
74
-
75
38
  private string DownloadString(string url)
76
-
77
39
  {
78
-
79
40
  using(var webClient = new System.Net.WebClient())
80
-
81
41
  {
82
-
83
42
  return webClient.DownloadString(url);
84
-
85
43
  }
86
-
87
44
  }
88
45
 
89
-
90
-
91
46
  private class Root
92
-
93
47
  {
94
-
95
48
  public List<Pair> quotes { get; set; }
96
-
97
49
  }
98
50
 
99
-
100
-
101
51
  private class Pair
102
-
103
52
  {
104
-
105
53
  public double high { get; set; }
106
-
107
54
  public double open { get; set; }
108
-
109
55
  public double bid { get; set; }
110
-
111
56
  public string currencyPairCode { get; set; }
112
-
113
57
  public double ask { get; set; }
114
-
115
58
  public double low { get; set; }
116
-
117
59
  }
118
-
119
60
  }
120
-
121
61
  }
122
-
123
62
  ```