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

質問編集履歴

1

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

2019/10/07 09:41

投稿

CoreiNion
CoreiNion

スコア5

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,6 @@
1
1
  Newtonsoft.Jsonを使ってJSONをデシアライズしようとしてますが、上手くできません。
2
2
  ビルドはうまくいきますが、以下の例外エラーが出てしまいます。
3
+ JSONの読み込みは上手くできています。
3
4
  ```
4
5
  **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.
5
6
  To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
@@ -7,37 +8,153 @@
7
8
  ```
8
9
  JSONに変換する部分はこのように書きました。
9
10
  ```C#
11
+ using System;
12
+ using System.Collections.Generic;
13
+ using System.Diagnostics;
14
+ using System.Text;
15
+ using System.Net.Http;
16
+ using Newtonsoft.Json;
17
+ using System.Threading.Tasks;
18
+ using System.Runtime.Serialization.Json;
19
+
20
+ namespace BEStatus
21
+ {
22
+ class GetApiData
23
+ {
24
+ //API情報
25
+ public string Token = "(Token key)";
26
+ public string Store_URL = "https://--";
27
+
28
+ //アイテムショップの処理
10
- public List<ShopItem> ShopList;
29
+ public List<ShopItem> ShopList;
11
30
  public async Task<List<ShopItem>> ShopItemAsync()
12
31
  {
13
32
  //List作成
14
33
  ShopList = new List<ShopItem>();
15
34
  HttpClient httpClient = new HttpClient();
35
+ httpClient.DefaultRequestHeaders.Add("Authorization", Token);
16
36
  //非同期でAPI取得
17
37
  Task<string> stringAsync = httpClient.GetStringAsync(Store_URL);
18
38
  string result = await stringAsync;
39
+ Debug.WriteLine(result);
19
40
  //Jsonに変換
20
41
  ShopList = JsonConvert.DeserializeObject<List<ShopItem>>(result);
21
42
  //Listでreturn
22
43
  return ShopList;
23
44
 
24
45
  }
46
+ }
47
+
48
+
49
+ public class ShopImages
50
+ {
51
+ public string icon { get; set; }
52
+ public string background { get; set; }
53
+ public string featured { get; set; }
54
+ public string fbackground { get; set; }
55
+ }
56
+
57
+ public class ShopItem
58
+ {
59
+ public string name { get; set; }
60
+ public string rarity { get; set; }
61
+ public string type { get; set; }
62
+ public ShopImages images { get; set; }
63
+ }
64
+
65
+ public class ShopStore
66
+ {
67
+ public int cost { get; set; }
68
+ public int regularCost { get; set; }
69
+ public bool isFeatured { get; set; }
70
+ public bool isNew { get; set; }
71
+ public bool isCommunityVote { get; set; }
72
+ }
73
+
74
+ public class ShopDatum
75
+ {
76
+ public string itemId { get; set; }
77
+ public ShopItem item { get; set; }
78
+ public ShopStore store { get; set; }
79
+ }
80
+
81
+ public class ShopRootObject
82
+ {
83
+ public List<ShopDatum> data { get; set; }
84
+ public int updateAt { get; set; }
85
+ }
86
+ }
25
87
  ```
88
+ 下のコードはリストを表示させるためのコードです(Xamarin)
89
+ ```C#
90
+ using System;
91
+ using System.Collections.Generic;
92
+ using System.Net.Http;
93
+ using Newtonsoft.Json;
94
+ using System.Threading.Tasks;
95
+ using System.Runtime.Serialization.Json;
96
+ using System.Text;
97
+ using Xamarin.Forms;
98
+ using Xamarin.Forms.Xaml;
99
+
100
+ namespace BEStatus
101
+ {
102
+ [XamlCompilation(XamlCompilationOptions.Compile)]
103
+ public partial class TodayItemShop : ContentPage
104
+ {
105
+
106
+ public ListView listView;
107
+ public List<ShopItem> ShopList;
108
+
109
+ public TodayItemShop()
110
+ {
111
+ //リストのデザイン系
112
+ InitializeComponent();
113
+
114
+ listView = new ListView
115
+ {
116
+ RowHeight = 60
117
+ };
118
+ Content = new StackLayout
119
+ {
120
+ VerticalOptions = LayoutOptions.FillAndExpand,
121
+ Children = { listView }
122
+ };
123
+
124
+ fetchArticles(new GetApiData());
125
+ }
126
+
127
+ //非同期でデータ取得
128
+ async void fetchArticles(GetApiData Api)
129
+ {
130
+
131
+ ShopList = await Api.ShopItemAsync();
132
+ var items = new List<String>();
133
+ foreach (ShopItem value in ShopList)
134
+ {
135
+ items.Add(value.name);
136
+ }
137
+ listView.ItemsSource = items;
138
+ }
139
+ }
140
+ }
141
+
142
+ ```
26
- 読み込むJSONの文字列はこんな感じです。(一部のみ)
143
+ 読み込むJSONの文字列はこんな感じです。
27
144
  ```JSON
28
145
  {
29
146
  "data": [
30
147
  {
31
- "itemId": "(ID)",
148
+ "itemId": "ID",
32
149
  "item": {
33
150
  "name": "Sunrise",
34
151
  "rarity": "rare",
35
152
  "type": "glider",
36
153
  "images": {
37
- "icon": "(URL)",
154
+ "icon": "URL",
38
- "background": "(URL)",
155
+ "background": "URL",
39
- "featured": "(URL)",
156
+ "featured": "URL",
40
- "fbackground": "(URL)"
157
+ "fbackground": "URL"
41
158
  }
42
159
  },
43
160
  "store": {
@@ -46,6 +163,96 @@
46
163
  "isFeatured": true,
47
164
  "isNew": false,
48
165
  "isCommunityVote": false
166
+ }
167
+ },
168
+ {
169
+ "itemId": "ID",
49
- .........
170
+ "item": {
171
+ "name": "Sky Stripe",
172
+ "rarity": "uncommon",
173
+ "type": "glider",
174
+ "images": {
175
+ "icon": "URL",
176
+ "background": "URL",
177
+ "featured": "URL",
178
+ "fbackground": "URL"
179
+ }
180
+ },
181
+ "store": {
182
+ "cost": 500,
183
+ "regularCost": 500,
184
+ "isFeatured": true,
185
+ "isNew": false,
186
+ "isCommunityVote": false
187
+ }
188
+ },
189
+ {
190
+ "itemId": "ID",
191
+ "item": {
192
+ "name": "Axetec",
193
+ "rarity": "rare",
194
+ "type": "pickaxe",
195
+ "images": {
196
+ "icon": "URL",
197
+ "background": "URL",
198
+ "featured": "URL",
199
+ "fbackground": "URL"
200
+ }
201
+ },
202
+ "store": {
203
+ "cost": 800,
204
+ "regularCost": 800,
205
+ "isFeatured": true,
206
+ "isNew": false,
207
+ "isCommunityVote": false
208
+ }
209
+ },
210
+ {
211
+ "itemId": "ID",
212
+ "item": {
213
+ "name": "Stripe Slicer",
214
+ "rarity": "uncommon",
215
+ "type": "pickaxe",
216
+ "images": {
217
+ "icon": "URL",
218
+ "background": "URL",
219
+ "featured": "URL",
220
+ "fbackground": "URL"
221
+ }
222
+ },
223
+ "store": {
224
+ "cost": 500,
225
+ "regularCost": 500,
226
+ "isFeatured": true,
227
+ "isNew": false,
228
+ "isCommunityVote": false
229
+ }
230
+ },
231
+ {
232
+ "itemId": "ID",
233
+ "item": {
234
+ "name": "Iron Beak",
235
+ "rarity": "rare",
236
+ "type": "pickaxe",
237
+ "images": {
238
+ "icon": "URL",
239
+ "background": "URL",
240
+ "featured": "URL",
241
+ "fbackground": "URL"
242
+ }
243
+ },
244
+ "store": {
245
+ "cost": 800,
246
+ "regularCost": 800,
247
+ "isFeatured": true,
248
+ "isNew": false,
249
+ "isCommunityVote": false
250
+ }
251
+
252
+
253
+ }
254
+ ],
255
+ "updateAt": 1570406481
256
+ }
50
257
  ```
51
258
  JSONはあまり触ったことがないので初歩的な質問かもしれませんが、回答よろしくお願いします。