回答編集履歴
3
追伸2を追加
test
CHANGED
@@ -49,3 +49,147 @@
|
|
49
49
|
json2csharp
|
50
50
|
|
51
51
|
[http://json2csharp.com/](http://json2csharp.com/)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
【追伸2】
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
「C# のクラス定義が違うのではないですか?」とか言っておいてハズレだったら何ですので、Json.NET を使って検証してみました。(MiniJSON というのは持ってないのでそれでどうやるかは分かりません)
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
Json.NET なら上に書いたクラス定義で逆シリアル化できることは確認できました。以下のコードを見てください。
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
using System;
|
72
|
+
|
73
|
+
using System.Collections.Generic;
|
74
|
+
|
75
|
+
using System.Linq;
|
76
|
+
|
77
|
+
using System.Text;
|
78
|
+
|
79
|
+
using Newtonsoft.Json;
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
namespace JsonNETSample
|
84
|
+
|
85
|
+
{
|
86
|
+
|
87
|
+
class Program
|
88
|
+
|
89
|
+
{
|
90
|
+
|
91
|
+
static void Main(string[] args)
|
92
|
+
|
93
|
+
{
|
94
|
+
|
95
|
+
string json = @"{""player_data"":[
|
96
|
+
|
97
|
+
{""id"":1,""name"":""Bob"",""point"":42},
|
98
|
+
|
99
|
+
{""id"":2,""name"":""Mary"",""point"":26},
|
100
|
+
|
101
|
+
{""id"":3,""name"":""Mike"",""point"":63},
|
102
|
+
|
103
|
+
{""id"":4,""name"":""Sayaka"",""point"":53},
|
104
|
+
|
105
|
+
{""id"":5,""name"":""Json"",""point"":64}]}";
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
foreach (PlayerData player in root.player_data)
|
114
|
+
|
115
|
+
{
|
116
|
+
|
117
|
+
Console.WriteLine("id:{0}, name:{1}, point:{2}",
|
118
|
+
|
119
|
+
player.id, player.name, player.point);
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
/*
|
126
|
+
|
127
|
+
結果は:
|
128
|
+
|
129
|
+
id:1, name:Bob, point:42
|
130
|
+
|
131
|
+
id:2, name:Mary, point:26
|
132
|
+
|
133
|
+
id:3, name:Mike, point:63
|
134
|
+
|
135
|
+
id:4, name:Sayaka, point:53
|
136
|
+
|
137
|
+
id:5, name:Json, point:64
|
138
|
+
|
139
|
+
*/
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
public class RootObject
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
public List<PlayerData> player_data { get; set; }
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
public class PlayerData
|
158
|
+
|
159
|
+
{
|
160
|
+
|
161
|
+
public int id { get; set; }
|
162
|
+
|
163
|
+
public string name { get; set; }
|
164
|
+
|
165
|
+
public int point { get; set; }
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
```
|
172
|
+
|
173
|
+
というわけで・・・
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
> 1.上のような仕様を満たすようなdecodeJsonクラス(途中までは下のソースコードに書いてあります)をどう書いていけば良いのでしょうか?(ヒント(?)が欲しいです
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
MiniJSON ではなく Json.NET を使った例ですが、上のコードを見てください。
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
> 2.下のソースコード内に登場するIList, IDictionaryとはList, Dictionaryとどのように違うのでしょうか?
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
IList, IDictionary など、頭に I がつくのはインターフェイスです。インターフェイスを継承したクラスはインターフェイスで定義されるメソッドを必ず実装するというルールになっています。
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
List<T>, Dictionary<TKey, TValue> はクラスです。それぞれ IList, IDictionary を継承しています。
|
194
|
+
|
195
|
+
|
2
追伸追加&リンク設定
test
CHANGED
@@ -48,4 +48,4 @@
|
|
48
48
|
|
49
49
|
json2csharp
|
50
50
|
|
51
|
-
http://json2csharp.com/
|
51
|
+
[http://json2csharp.com/](http://json2csharp.com/)
|
1
追伸追加
test
CHANGED
@@ -35,3 +35,17 @@
|
|
35
35
|
}
|
36
36
|
|
37
37
|
```
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
【追伸】
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
「JSON 文字列」⇒「C# のクラス定義」の変換サービスがあるので使ってみてください。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
json2csharp
|
50
|
+
|
51
|
+
http://json2csharp.com/
|