我是Alamofire的新手,我測試了一些API。我遇到了匯率。所以JSON文件是這樣Alamofire 4.0 JSOn解析Swift
["base": CAD, "date": 2016-12-01, "rates": {
AUD = "1.0097";
BGN = "1.3735";
BRL = "2.57";
CHF = "0.7559";
CNY = "5.1388";
CZK = "19.004";
DKK = "5.2248";
EUR = "0.70225";
GBP = "0.59058";
HKD = "5.7881";
HRK = "5.2985";
HUF = "220.48";
IDR = 10108;
ILS = "2.8607";
INR = "51.009";
JPY = "85.246";
KRW = "871.9400000000001";
MXN = "15.403";
MYR = "3.331";
NOK = "6.2941";
NZD = "1.0539";
PHP = "37.102";
PLN = "3.1374";
RON = "3.1631";
RUB = "47.591";
SEK = "6.8775";
SGD = "1.0657";
THB = "26.616";
TRY = "2.6006";
USD = "0.7462800000000001";
ZAR = "10.504";}]
typealias JSONStandard = [String: AnyObject]
func parseData(JSONData:Data) {
do {
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
if let rates = readableJSON["rates"] as? JSONStandard{
for i in 0..<rates.count {
let rate = rates[i] as! //Here
}
print(rates)
}
print(readableJSON)
} catch {
print(error)
}
}
我能得到側「差餉」,但我不明白我怎麼會解析裏面的「速率」中的所有數據。我以爲我必須把它保存在字典裏面。很迷茫謝謝
這甚至不是一個有效的數據集。它不應該包含分號。 –
@ElTomato這實際上是如此真實,我只是意識到這一點。但我確信有一種解決方法 –
沒有辦法繞過它。您當然可以手動修復您的JSON數據集並將其作爲Swift文件讀取。 –