質問編集履歴

1

コードをすべて記述、補足

2019/10/07 09:41

投稿

CoreiNion
CoreiNion

スコア5

test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ビルドはうまくいきますが、以下の例外エラーが出てしまいます。
4
4
 
5
+ JSONの読み込みは上手くできています。
6
+
5
7
  ```
6
8
 
7
9
  **Newtonsoft.Json.JsonSerializationException:** 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[BEStatus.ShopItem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
@@ -16,7 +18,43 @@
16
18
 
17
19
  ```C#
18
20
 
21
+ using System;
22
+
23
+ using System.Collections.Generic;
24
+
25
+ using System.Diagnostics;
26
+
27
+ using System.Text;
28
+
29
+ using System.Net.Http;
30
+
31
+ using Newtonsoft.Json;
32
+
33
+ using System.Threading.Tasks;
34
+
35
+ using System.Runtime.Serialization.Json;
36
+
37
+
38
+
39
+ namespace BEStatus
40
+
41
+ {
42
+
43
+ class GetApiData
44
+
45
+ {
46
+
47
+ //API情報
48
+
49
+ public string Token = "(Token key)";
50
+
51
+ public string Store_URL = "https://--";
52
+
53
+
54
+
55
+ //アイテムショップの処理
56
+
19
- public List<ShopItem> ShopList;
57
+ public List<ShopItem> ShopList;
20
58
 
21
59
  public async Task<List<ShopItem>> ShopItemAsync()
22
60
 
@@ -28,12 +66,16 @@
28
66
 
29
67
  HttpClient httpClient = new HttpClient();
30
68
 
69
+ httpClient.DefaultRequestHeaders.Add("Authorization", Token);
70
+
31
71
  //非同期でAPI取得
32
72
 
33
73
  Task<string> stringAsync = httpClient.GetStringAsync(Store_URL);
34
74
 
35
75
  string result = await stringAsync;
36
76
 
77
+ Debug.WriteLine(result);
78
+
37
79
  //Jsonに変換
38
80
 
39
81
  ShopList = JsonConvert.DeserializeObject<List<ShopItem>>(result);
@@ -46,9 +88,201 @@
46
88
 
47
89
  }
48
90
 
91
+ }
92
+
93
+
94
+
95
+
96
+
97
+ public class ShopImages
98
+
99
+ {
100
+
101
+ public string icon { get; set; }
102
+
103
+ public string background { get; set; }
104
+
105
+ public string featured { get; set; }
106
+
107
+ public string fbackground { get; set; }
108
+
109
+ }
110
+
111
+
112
+
113
+ public class ShopItem
114
+
115
+ {
116
+
117
+ public string name { get; set; }
118
+
119
+ public string rarity { get; set; }
120
+
121
+ public string type { get; set; }
122
+
123
+ public ShopImages images { get; set; }
124
+
125
+ }
126
+
127
+
128
+
129
+ public class ShopStore
130
+
131
+ {
132
+
133
+ public int cost { get; set; }
134
+
135
+ public int regularCost { get; set; }
136
+
137
+ public bool isFeatured { get; set; }
138
+
139
+ public bool isNew { get; set; }
140
+
141
+ public bool isCommunityVote { get; set; }
142
+
143
+ }
144
+
145
+
146
+
147
+ public class ShopDatum
148
+
149
+ {
150
+
151
+ public string itemId { get; set; }
152
+
153
+ public ShopItem item { get; set; }
154
+
155
+ public ShopStore store { get; set; }
156
+
157
+ }
158
+
159
+
160
+
161
+ public class ShopRootObject
162
+
163
+ {
164
+
165
+ public List<ShopDatum> data { get; set; }
166
+
167
+ public int updateAt { get; set; }
168
+
169
+ }
170
+
171
+ }
172
+
49
173
  ```
50
174
 
175
+ 下のコードはリストを表示させるためのコードです(Xamarin)
176
+
177
+ ```C#
178
+
179
+ using System;
180
+
181
+ using System.Collections.Generic;
182
+
183
+ using System.Net.Http;
184
+
185
+ using Newtonsoft.Json;
186
+
187
+ using System.Threading.Tasks;
188
+
189
+ using System.Runtime.Serialization.Json;
190
+
191
+ using System.Text;
192
+
193
+ using Xamarin.Forms;
194
+
195
+ using Xamarin.Forms.Xaml;
196
+
197
+
198
+
199
+ namespace BEStatus
200
+
201
+ {
202
+
203
+ [XamlCompilation(XamlCompilationOptions.Compile)]
204
+
205
+ public partial class TodayItemShop : ContentPage
206
+
207
+ {
208
+
209
+
210
+
211
+ public ListView listView;
212
+
213
+ public List<ShopItem> ShopList;
214
+
215
+
216
+
217
+ public TodayItemShop()
218
+
219
+ {
220
+
221
+ //リストのデザイン系
222
+
223
+ InitializeComponent();
224
+
225
+
226
+
227
+ listView = new ListView
228
+
229
+ {
230
+
231
+ RowHeight = 60
232
+
233
+ };
234
+
235
+ Content = new StackLayout
236
+
237
+ {
238
+
239
+ VerticalOptions = LayoutOptions.FillAndExpand,
240
+
241
+ Children = { listView }
242
+
243
+ };
244
+
245
+
246
+
247
+ fetchArticles(new GetApiData());
248
+
249
+ }
250
+
251
+
252
+
253
+ //非同期でデータ取得
254
+
255
+ async void fetchArticles(GetApiData Api)
256
+
257
+ {
258
+
259
+
260
+
261
+ ShopList = await Api.ShopItemAsync();
262
+
263
+ var items = new List<String>();
264
+
265
+ foreach (ShopItem value in ShopList)
266
+
267
+ {
268
+
269
+ items.Add(value.name);
270
+
271
+ }
272
+
273
+ listView.ItemsSource = items;
274
+
275
+ }
276
+
277
+ }
278
+
279
+ }
280
+
281
+
282
+
283
+ ```
284
+
51
- 読み込むJSONの文字列はこんな感じです。(一部のみ)
285
+ 読み込むJSONの文字列はこんな感じです。
52
286
 
53
287
  ```JSON
54
288
 
@@ -58,7 +292,7 @@
58
292
 
59
293
  {
60
294
 
61
- "itemId": "(ID)",
295
+ "itemId": "ID",
62
296
 
63
297
  "item": {
64
298
 
@@ -70,13 +304,13 @@
70
304
 
71
305
  "images": {
72
306
 
73
- "icon": "(URL)",
307
+ "icon": "URL",
74
-
308
+
75
- "background": "(URL)",
309
+ "background": "URL",
76
-
310
+
77
- "featured": "(URL)",
311
+ "featured": "URL",
78
-
312
+
79
- "fbackground": "(URL)"
313
+ "fbackground": "URL"
80
314
 
81
315
  }
82
316
 
@@ -94,7 +328,187 @@
94
328
 
95
329
  "isCommunityVote": false
96
330
 
331
+ }
332
+
333
+ },
334
+
335
+ {
336
+
337
+ "itemId": "ID",
338
+
97
- .........
339
+ "item": {
340
+
341
+ "name": "Sky Stripe",
342
+
343
+ "rarity": "uncommon",
344
+
345
+ "type": "glider",
346
+
347
+ "images": {
348
+
349
+ "icon": "URL",
350
+
351
+ "background": "URL",
352
+
353
+ "featured": "URL",
354
+
355
+ "fbackground": "URL"
356
+
357
+ }
358
+
359
+ },
360
+
361
+ "store": {
362
+
363
+ "cost": 500,
364
+
365
+ "regularCost": 500,
366
+
367
+ "isFeatured": true,
368
+
369
+ "isNew": false,
370
+
371
+ "isCommunityVote": false
372
+
373
+ }
374
+
375
+ },
376
+
377
+ {
378
+
379
+ "itemId": "ID",
380
+
381
+ "item": {
382
+
383
+ "name": "Axetec",
384
+
385
+ "rarity": "rare",
386
+
387
+ "type": "pickaxe",
388
+
389
+ "images": {
390
+
391
+ "icon": "URL",
392
+
393
+ "background": "URL",
394
+
395
+ "featured": "URL",
396
+
397
+ "fbackground": "URL"
398
+
399
+ }
400
+
401
+ },
402
+
403
+ "store": {
404
+
405
+ "cost": 800,
406
+
407
+ "regularCost": 800,
408
+
409
+ "isFeatured": true,
410
+
411
+ "isNew": false,
412
+
413
+ "isCommunityVote": false
414
+
415
+ }
416
+
417
+ },
418
+
419
+ {
420
+
421
+ "itemId": "ID",
422
+
423
+ "item": {
424
+
425
+ "name": "Stripe Slicer",
426
+
427
+ "rarity": "uncommon",
428
+
429
+ "type": "pickaxe",
430
+
431
+ "images": {
432
+
433
+ "icon": "URL",
434
+
435
+ "background": "URL",
436
+
437
+ "featured": "URL",
438
+
439
+ "fbackground": "URL"
440
+
441
+ }
442
+
443
+ },
444
+
445
+ "store": {
446
+
447
+ "cost": 500,
448
+
449
+ "regularCost": 500,
450
+
451
+ "isFeatured": true,
452
+
453
+ "isNew": false,
454
+
455
+ "isCommunityVote": false
456
+
457
+ }
458
+
459
+ },
460
+
461
+ {
462
+
463
+ "itemId": "ID",
464
+
465
+ "item": {
466
+
467
+ "name": "Iron Beak",
468
+
469
+ "rarity": "rare",
470
+
471
+ "type": "pickaxe",
472
+
473
+ "images": {
474
+
475
+ "icon": "URL",
476
+
477
+ "background": "URL",
478
+
479
+ "featured": "URL",
480
+
481
+ "fbackground": "URL"
482
+
483
+ }
484
+
485
+ },
486
+
487
+ "store": {
488
+
489
+ "cost": 800,
490
+
491
+ "regularCost": 800,
492
+
493
+ "isFeatured": true,
494
+
495
+ "isNew": false,
496
+
497
+ "isCommunityVote": false
498
+
499
+ }
500
+
501
+
502
+
503
+
504
+
505
+ }
506
+
507
+ ],
508
+
509
+ "updateAt": 1570406481
510
+
511
+ }
98
512
 
99
513
  ```
100
514