teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

追記

2021/04/01 00:59

投稿

退会済みユーザー
answer CHANGED
@@ -10,6 +10,10 @@
10
10
  自動生成されたクラス名の Rootobject だけ EarthQuake に変えました。(使った既存のサンプルの他のコードとダブるので)
11
11
 
12
12
  ```
13
+ namespace NewtonsoftJson
14
+
15
+ // ・・・中略・・・
16
+
13
17
  public class EarthQuake // Rootobject から変更
14
18
  {
15
19
  public string ParseStatus { get; set; }
@@ -153,8 +157,9 @@
153
157
  public int Code { get; set; }
154
158
  public string String { get; set; }
155
159
  }
160
+ }
156
161
  ```
157
162
 
158
- 上のクラス定義を使って以下のようにデシリアライズできます。
163
+ 上のクラス定義を使って以下のようにデシリアライズできます。(画像に出てくる NewtonsoftJson というのは名前空間名です)
159
164
 
160
165
  ![イメージ説明](80d0b1cef4a495c639709039b614572d.jpeg)

1

追記

2021/04/01 00:59

投稿

退会済みユーザー
answer CHANGED
@@ -1,4 +1,160 @@
1
1
  以下の記事のように Visual Studio の機能を利用して JSON 文字列から C# のクラス定義を作成して、JSON 文字列をそれにデシリアライズしてみてください。
2
2
 
3
3
  JSON 文字列から C# のクラス定義生成
4
- [http://surferonwww.info/BlogEngine/post/2020/05/10/generate-class-definition-from-json-string.aspx](http://surferonwww.info/BlogEngine/post/2020/05/10/generate-class-definition-from-json-string.aspx)
4
+ [http://surferonwww.info/BlogEngine/post/2020/05/10/generate-class-definition-from-json-string.aspx](http://surferonwww.info/BlogEngine/post/2020/05/10/generate-class-definition-from-json-string.aspx)
5
+
6
+ **【2021/4/1 9:55 追記】**
7
+
8
+ https://api.iedred7584.com/eew/json/ から取得できる JSON 文字列から、上の記事で紹介したように VS2019 の機能を使って C# のオブジェクトを生成すると以下のようになります。
9
+
10
+ 自動生成されたクラス名の Rootobject だけ EarthQuake に変えました。(使った既存のサンプルの他のコードとダブるので)
11
+
12
+ ```
13
+ public class EarthQuake // Rootobject から変更
14
+ {
15
+ public string ParseStatus { get; set; }
16
+ public Title Title { get; set; }
17
+ public Source Source { get; set; }
18
+ public Status Status { get; set; }
19
+ public Announcedtime AnnouncedTime { get; set; }
20
+ public Origintime OriginTime { get; set; }
21
+ public string EventID { get; set; }
22
+ public Type Type { get; set; }
23
+ public int Serial { get; set; }
24
+ public Hypocenter Hypocenter { get; set; }
25
+ public Maxintensity MaxIntensity { get; set; }
26
+ public bool Warn { get; set; }
27
+ public Option Option { get; set; }
28
+ public string OriginalText { get; set; }
29
+ }
30
+
31
+ public class Title
32
+ {
33
+ public int Code { get; set; }
34
+ public string String { get; set; }
35
+ public string Detail { get; set; }
36
+ }
37
+
38
+ public class Source
39
+ {
40
+ public int Code { get; set; }
41
+ public string String { get; set; }
42
+ }
43
+
44
+ public class Status
45
+ {
46
+ public string Code { get; set; }
47
+ public string String { get; set; }
48
+ public string Detail { get; set; }
49
+ }
50
+
51
+ public class Announcedtime
52
+ {
53
+ public string String { get; set; }
54
+ public int UnixTime { get; set; }
55
+ public string RFC1123 { get; set; }
56
+ }
57
+
58
+ public class Origintime
59
+ {
60
+ public string String { get; set; }
61
+ public int UnixTime { get; set; }
62
+ public string RFC1123 { get; set; }
63
+ }
64
+
65
+ public class Type
66
+ {
67
+ public int Code { get; set; }
68
+ public string String { get; set; }
69
+ public string Detail { get; set; }
70
+ }
71
+
72
+ public class Hypocenter
73
+ {
74
+ public int Code { get; set; }
75
+ public string Name { get; set; }
76
+ public bool isAssumption { get; set; }
77
+ public Location Location { get; set; }
78
+ public Magnitude Magnitude { get; set; }
79
+ public Accuracy Accuracy { get; set; }
80
+ public bool isSea { get; set; }
81
+ }
82
+
83
+ public class Location
84
+ {
85
+ public float Lat { get; set; }
86
+ public float Long { get; set; }
87
+ public Depth Depth { get; set; }
88
+ }
89
+
90
+ public class Depth
91
+ {
92
+ public int Int { get; set; }
93
+ public string String { get; set; }
94
+ }
95
+
96
+ public class Magnitude
97
+ {
98
+ public float Float { get; set; }
99
+ public string String { get; set; }
100
+ public string LongString { get; set; }
101
+ }
102
+
103
+ public class Accuracy
104
+ {
105
+ public Epicenter Epicenter { get; set; }
106
+ public Depth1 Depth { get; set; }
107
+ public Magnitude1 Magnitude { get; set; }
108
+ public int NumberOfMagnitudeCalculation { get; set; }
109
+ }
110
+
111
+ public class Epicenter
112
+ {
113
+ public int Code { get; set; }
114
+ public string String { get; set; }
115
+ public int Rank2 { get; set; }
116
+ public string String2 { get; set; }
117
+ }
118
+
119
+ public class Depth1
120
+ {
121
+ public int Code { get; set; }
122
+ public string String { get; set; }
123
+ }
124
+
125
+ public class Magnitude1
126
+ {
127
+ public int Code { get; set; }
128
+ public string String { get; set; }
129
+ }
130
+
131
+ public class Maxintensity
132
+ {
133
+ public string From { get; set; }
134
+ public string To { get; set; }
135
+ public string String { get; set; }
136
+ public string LongString { get; set; }
137
+ }
138
+
139
+ public class Option
140
+ {
141
+ public Change Change { get; set; }
142
+ }
143
+
144
+ public class Change
145
+ {
146
+ public int Code { get; set; }
147
+ public string String { get; set; }
148
+ public Reason Reason { get; set; }
149
+ }
150
+
151
+ public class Reason
152
+ {
153
+ public int Code { get; set; }
154
+ public string String { get; set; }
155
+ }
156
+ ```
157
+
158
+ 上のクラス定義を使って以下のようにデシリアライズできます。
159
+
160
+ ![イメージ説明](80d0b1cef4a495c639709039b614572d.jpeg)