回答編集履歴
2
レスポンスのデータ型で処理を分岐
test
CHANGED
@@ -97,3 +97,97 @@
|
|
97
97
|
|
98
98
|
|
99
99
|
ご参考にしてください。
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
---
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
これで動くでしょう。
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
以下のAPIで動作を確認しました。
|
114
|
+
|
115
|
+
`https://tepco-usage-api.appspot.com/latest.json`
|
116
|
+
|
117
|
+
`https://qiita.com/api/v2/items`
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
`SwiftyJSON`や`Argo`のようなJSONパーサを利用したほうがきっと幸せになれるでしょう。
|
122
|
+
|
123
|
+
[SwiftyJSON/SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON)
|
124
|
+
|
125
|
+
[thoughtbot/Argo](https://github.com/thoughtbot/Argo)
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
```swift
|
130
|
+
|
131
|
+
.responseJSON { response in
|
132
|
+
|
133
|
+
switch response.result {
|
134
|
+
|
135
|
+
// データを取り出す
|
136
|
+
|
137
|
+
case .Success(let value):
|
138
|
+
|
139
|
+
// print(value)
|
140
|
+
|
141
|
+
let dataArray = NSMutableArray()
|
142
|
+
|
143
|
+
var dict = NSDictionary()
|
144
|
+
|
145
|
+
// レスポンスのデータ型を確認
|
146
|
+
|
147
|
+
if value is NSDictionary {
|
148
|
+
|
149
|
+
// print("value is Dictionary type.")
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
dict = value as! NSDictionary
|
154
|
+
|
155
|
+
dataArray.addObject(dict)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
} else if value is NSArray {
|
160
|
+
|
161
|
+
// print("value is Array type.")
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
let count = value.count
|
166
|
+
|
167
|
+
for(var i = 0; i < count; ++i) {
|
168
|
+
|
169
|
+
dict = value[i] as! NSDictionary
|
170
|
+
|
171
|
+
dataArray.addObject(dict)
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
print("データ数: \(dataArray.count)")
|
178
|
+
|
179
|
+
print(dataArray[0])
|
180
|
+
|
181
|
+
// print(dataArray[0]["id"] as! Int)
|
182
|
+
|
183
|
+
// 失敗時エラー出力
|
184
|
+
|
185
|
+
case .Failure(let error):
|
186
|
+
|
187
|
+
print(error)
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
```
|
1
記述の変更、他アクセス先でのケース
test
CHANGED
@@ -15,3 +15,85 @@
|
|
15
15
|
print(name)
|
16
16
|
|
17
17
|
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
---
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
以下のように書くとどのような出力になりますか。
|
26
|
+
|
27
|
+
```swift
|
28
|
+
|
29
|
+
.responseJSON { response in
|
30
|
+
|
31
|
+
switch response.result {
|
32
|
+
|
33
|
+
// データを取り出す
|
34
|
+
|
35
|
+
case .Success(let value):
|
36
|
+
|
37
|
+
print(value)
|
38
|
+
|
39
|
+
// 失敗時エラー出力
|
40
|
+
|
41
|
+
case .Failure(let error):
|
42
|
+
|
43
|
+
print(error)
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
また、`requestUrl`を変更しても出力に違いが見られませんか。
|
54
|
+
|
55
|
+
`requestUrl`を`https://tepco-usage-api.appspot.com/latest.json`にすると、私の環境では以下のような出力になります。
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
```json
|
60
|
+
|
61
|
+
{
|
62
|
+
|
63
|
+
capacity = 4214;
|
64
|
+
|
65
|
+
"capacity_peak_period" = 17;
|
66
|
+
|
67
|
+
"capacity_updated" = "2012-12-24 08:30:00";
|
68
|
+
|
69
|
+
day = 25;
|
70
|
+
|
71
|
+
entryfor = "2015-12-24 16:00:00";
|
72
|
+
|
73
|
+
forecast = 0;
|
74
|
+
|
75
|
+
"forecast_peak_period" = 17;
|
76
|
+
|
77
|
+
"forecast_peak_updated" = "2012-12-24 08:30:00";
|
78
|
+
|
79
|
+
"forecast_peak_usage" = 3620;
|
80
|
+
|
81
|
+
hour = 1;
|
82
|
+
|
83
|
+
month = 12;
|
84
|
+
|
85
|
+
saving = 0;
|
86
|
+
|
87
|
+
usage = 2623;
|
88
|
+
|
89
|
+
"usage_updated" = "2015-12-24 17:05:06";
|
90
|
+
|
91
|
+
year = 2015;
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
ご参考にしてください。
|