質問編集履歴
2
詳細を修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
json形式への変化処理
|
1
|
+
json形式への変化処理 swift
|
test
CHANGED
@@ -1,27 +1,243 @@
|
|
1
|
-
##
|
2
|
-
|
3
|
-
```
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
1
|
+
##API
|
2
|
+
|
3
|
+
```
|
4
|
+
|
5
|
+
{
|
6
|
+
|
7
|
+
result: [
|
8
|
+
|
9
|
+
{
|
10
|
+
|
11
|
+
foodImageUrl: "https://image.space.rakuten.co.jp/d/strg/ctrl/3/fbd7dd260d736654532e6c0b1ec185a0cede8675.49.2.3.2.jpg",
|
12
|
+
|
13
|
+
recipeDescription: "そのままでも、ご飯にのせて丼にしても♪",
|
14
|
+
|
15
|
+
recipePublishday: "2017/10/10 22:37:34",
|
16
|
+
|
17
|
+
shop: 0,
|
18
|
+
|
19
|
+
pickup: 0,
|
20
|
+
|
21
|
+
recipeId: 1760028309,
|
22
|
+
|
23
|
+
nickname: "はぁぽじ",
|
24
|
+
|
25
|
+
smallImageUrl: "https://image.space.rakuten.co.jp/d/strg/ctrl/3/fbd7dd260d736654532e6c0b1ec185a0cede8675.49.2.3.2.jpg?thum=55",
|
26
|
+
|
27
|
+
recipeMaterial: [
|
28
|
+
|
29
|
+
"鶏むね肉",
|
30
|
+
|
31
|
+
"塩",
|
32
|
+
|
33
|
+
"酒",
|
34
|
+
|
35
|
+
"片栗粉",
|
36
|
+
|
37
|
+
"○水",
|
38
|
+
|
39
|
+
"○塩",
|
40
|
+
|
41
|
+
"○鶏がらスープの素",
|
42
|
+
|
43
|
+
"○黒胡椒",
|
44
|
+
|
45
|
+
"長ネギ",
|
46
|
+
|
47
|
+
"いりごま",
|
48
|
+
|
49
|
+
"ごま油"
|
50
|
+
|
51
|
+
],
|
52
|
+
|
53
|
+
recipeIndication: "約10分",
|
54
|
+
|
55
|
+
recipeCost: "300円前後",
|
56
|
+
|
57
|
+
rank: "1",
|
58
|
+
|
59
|
+
recipeUrl: "https://recipe.rakuten.co.jp/recipe/1760028309/",
|
60
|
+
|
61
|
+
mediumImageUrl: "https://image.space.rakuten.co.jp/d/strg/ctrl/3/fbd7dd260d736654532e6c0b1ec185a0cede8675.49.2.3.2.jpg?thum=54",
|
62
|
+
|
63
|
+
recipeTitle: "ご飯がすすむ!鶏むね肉のねぎ塩焼き"
|
64
|
+
|
65
|
+
},
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
##やりたいこと
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
foodImageUrlをとってtableviewに表示させたい。
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
##試したコード
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
import UIKit
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
struct ResultUserList: Codable {
|
86
|
+
|
87
|
+
var result: User
|
88
|
+
|
89
|
+
struct User: Codable{
|
90
|
+
|
91
|
+
var foodImageUrl: String
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
class ResultViewModel {
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
static func fetchArticle(completion: @escaping ([ResultUserList]) -> Swift.Void)
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
let url = "https://app.rakuten.co.jp/services/api/Recipe/CategoryRanking/20170426?format=json&applicationId=1095609837981916624"
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
guard var urlComponents = URLComponents(string: url) else {
|
112
|
+
|
113
|
+
return
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
//URLQueryItemを使うことでURLのクエリストリングを追加して、ページの要素数を50までと指定
|
120
|
+
|
121
|
+
urlComponents.queryItems = [
|
122
|
+
|
123
|
+
URLQueryItem(name: "per_page", value: "50"),
|
124
|
+
|
125
|
+
]
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
let task = URLSession.shared.dataTask(with: urlComponents.url!) { data, response, error in
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
guard let jsonData = data else {
|
20
134
|
|
21
135
|
return
|
22
136
|
|
23
137
|
}
|
24
138
|
|
139
|
+
do{
|
140
|
+
|
141
|
+
let articles = try JSONDecoder().decode([ResultUserList].self, from: jsonData)
|
142
|
+
|
143
|
+
completion(articles)
|
144
|
+
|
145
|
+
} catch {
|
146
|
+
|
147
|
+
print(error.localizedDescription)
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
task.resume()
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
class ViewController: UIViewController {
|
162
|
+
|
163
|
+
private var tableView = UITableView()
|
164
|
+
|
165
|
+
fileprivate var articles: [ResultUserList] = []
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
override func viewDidLoad() {
|
170
|
+
|
171
|
+
super.viewDidLoad()
|
172
|
+
|
173
|
+
self.title = "レシピ"
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
tableView.dataSource = self
|
178
|
+
|
179
|
+
tableView.frame = view.frame
|
180
|
+
|
181
|
+
view.addSubview(tableView)
|
182
|
+
|
183
|
+
ResultViewModel.fetchArticle(completion: { (articles) in
|
184
|
+
|
185
|
+
self.articles = articles
|
186
|
+
|
187
|
+
DispatchQueue.main.async {
|
188
|
+
|
189
|
+
self.tableView.reloadData()
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
})
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
override func didReceiveMemoryWarning() {
|
198
|
+
|
199
|
+
super.didReceiveMemoryWarning()
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
extension ViewController: UITableViewDataSource {
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
216
|
+
|
217
|
+
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
|
218
|
+
|
219
|
+
let article = articles[indexPath.row]
|
220
|
+
|
221
|
+
cell.textLabel?.text = article.result.foodImageUrl
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
return cell
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
232
|
+
|
233
|
+
return articles.count
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
25
241
|
```
|
26
242
|
|
27
243
|
|
1
使用言語のタグつけ
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|