質問編集履歴
2
詳細を修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
json形式への変化処理
|
1
|
+
json形式への変化処理 swift
|
body
CHANGED
@@ -1,15 +1,123 @@
|
|
1
|
-
##
|
1
|
+
##API
|
2
2
|
```
|
3
|
-
|
3
|
+
{
|
4
|
+
result: [
|
5
|
+
{
|
6
|
+
foodImageUrl: "https://image.space.rakuten.co.jp/d/strg/ctrl/3/fbd7dd260d736654532e6c0b1ec185a0cede8675.49.2.3.2.jpg",
|
7
|
+
recipeDescription: "そのままでも、ご飯にのせて丼にしても♪",
|
8
|
+
recipePublishday: "2017/10/10 22:37:34",
|
9
|
+
shop: 0,
|
10
|
+
pickup: 0,
|
11
|
+
recipeId: 1760028309,
|
12
|
+
nickname: "はぁぽじ",
|
13
|
+
smallImageUrl: "https://image.space.rakuten.co.jp/d/strg/ctrl/3/fbd7dd260d736654532e6c0b1ec185a0cede8675.49.2.3.2.jpg?thum=55",
|
14
|
+
recipeMaterial: [
|
15
|
+
"鶏むね肉",
|
16
|
+
"塩",
|
17
|
+
"酒",
|
18
|
+
"片栗粉",
|
19
|
+
"○水",
|
20
|
+
"○塩",
|
21
|
+
"○鶏がらスープの素",
|
22
|
+
"○黒胡椒",
|
23
|
+
"長ネギ",
|
24
|
+
"いりごま",
|
25
|
+
"ごま油"
|
26
|
+
],
|
27
|
+
recipeIndication: "約10分",
|
28
|
+
recipeCost: "300円前後",
|
29
|
+
rank: "1",
|
30
|
+
recipeUrl: "https://recipe.rakuten.co.jp/recipe/1760028309/",
|
31
|
+
mediumImageUrl: "https://image.space.rakuten.co.jp/d/strg/ctrl/3/fbd7dd260d736654532e6c0b1ec185a0cede8675.49.2.3.2.jpg?thum=54",
|
32
|
+
recipeTitle: "ご飯がすすむ!鶏むね肉のねぎ塩焼き"
|
33
|
+
},
|
4
34
|
```
|
5
|
-
##
|
35
|
+
##やりたいこと
|
6
36
|
```
|
37
|
+
foodImageUrlをとってtableviewに表示させたい。
|
38
|
+
```
|
39
|
+
##試したコード
|
40
|
+
```
|
41
|
+
import UIKit
|
42
|
+
|
7
|
-
|
43
|
+
struct ResultUserList: Codable {
|
8
|
-
guard let jsonData = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: Any] else {
|
9
|
-
// 変換失敗
|
10
|
-
|
44
|
+
var result: User
|
45
|
+
struct User: Codable{
|
46
|
+
var foodImageUrl: String
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
class ResultViewModel {
|
51
|
+
|
52
|
+
static func fetchArticle(completion: @escaping ([ResultUserList]) -> Swift.Void)
|
53
|
+
{
|
54
|
+
let url = "https://app.rakuten.co.jp/services/api/Recipe/CategoryRanking/20170426?format=json&applicationId=1095609837981916624"
|
55
|
+
|
56
|
+
guard var urlComponents = URLComponents(string: url) else {
|
57
|
+
return
|
58
|
+
}
|
59
|
+
|
60
|
+
//URLQueryItemを使うことでURLのクエリストリングを追加して、ページの要素数を50までと指定
|
61
|
+
urlComponents.queryItems = [
|
62
|
+
URLQueryItem(name: "per_page", value: "50"),
|
63
|
+
]
|
64
|
+
|
65
|
+
let task = URLSession.shared.dataTask(with: urlComponents.url!) { data, response, error in
|
66
|
+
|
67
|
+
guard let jsonData = data else {
|
11
68
|
return
|
12
69
|
}
|
70
|
+
do{
|
71
|
+
let articles = try JSONDecoder().decode([ResultUserList].self, from: jsonData)
|
72
|
+
completion(articles)
|
73
|
+
} catch {
|
74
|
+
print(error.localizedDescription)
|
75
|
+
}
|
76
|
+
}
|
77
|
+
task.resume()
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
class ViewController: UIViewController {
|
82
|
+
private var tableView = UITableView()
|
83
|
+
fileprivate var articles: [ResultUserList] = []
|
84
|
+
|
85
|
+
override func viewDidLoad() {
|
86
|
+
super.viewDidLoad()
|
87
|
+
self.title = "レシピ"
|
88
|
+
|
89
|
+
tableView.dataSource = self
|
90
|
+
tableView.frame = view.frame
|
91
|
+
view.addSubview(tableView)
|
92
|
+
ResultViewModel.fetchArticle(completion: { (articles) in
|
93
|
+
self.articles = articles
|
94
|
+
DispatchQueue.main.async {
|
95
|
+
self.tableView.reloadData()
|
96
|
+
}
|
97
|
+
})
|
98
|
+
}
|
99
|
+
override func didReceiveMemoryWarning() {
|
100
|
+
super.didReceiveMemoryWarning()
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
extension ViewController: UITableViewDataSource {
|
107
|
+
|
108
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
109
|
+
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
|
110
|
+
let article = articles[indexPath.row]
|
111
|
+
cell.textLabel?.text = article.result.foodImageUrl
|
112
|
+
|
113
|
+
return cell
|
114
|
+
}
|
115
|
+
|
116
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
117
|
+
return articles.count
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
13
121
|
```
|
14
122
|
|
15
123
|
よろしくお願いします。
|
1
使用言語のタグつけ
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|