CTAの列車到着のJSONを解析しようとしています。私ViewController.swift
JSON Swift 3 CTA Train Trackerの構文解析
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=individualkeygoeshere&mapid=41320&outputType=JSON")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print("Error")
}
else {
if let content = data
{
do {
let myJSON = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let ctattimetable = myJSON["ctatt"] as? [String:Any] {
if let estArrivalTime = ctattimetable["eta"] as? [Any] {
print(estArrivalTime)
if let stationName = estArrivalTime["staNm"] {
print(stationName)
}
}
}
}
catch {
}
}
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
で
{
ctatt = {
errCd = 0;
errNm = "<null>";
eta = (
{
arrT = "2017-05-15T23:22:32";
destNm = Howard;
destSt = 30173;
flags = "<null>";
heading = 305;
isApp = 0;
isDly = 0;
isFlt = 0;
isSch = 0;
lat = "41.91065";
lon = "-87.64918";
prdt = "2017-05-15T23:18:32";
rn = 843;
rt = Red;
staId = 41320;
staNm = Belmont;
stpDe = "Service toward Howard";
stpId = 30255;
trDr = 1;
},
{
arrT = "2017-05-15T23:21:45";
destNm = "95th/Dan Ryan";
destSt = 30089;
flags = "<null>";
heading = 91;
isApp = 0;
isDly = 0;
isFlt = 0;
isSch = 0;
lat = "41.95377";
lon = "-87.65493";
prdt = "2017-05-15T23:17:45";
rn = 836;
rt = Red;
staId = 41320;
staNm = Belmont;
stpDe = "Service toward 95th/Dan Ryan";
stpId = 30256;
trDr = 5;
},
{
arrT = "2017-05-15T23:27:28";
destNm = Kimball;
destSt = 30249;
flags = "<null>";
heading = 357;
isApp = 0;
isDly = 0;
isFlt = 0;
isSch = 0;
lat = "41.90563";
lon = "-87.63712";
prdt = "2017-05-15T23:18:28";
rn = 426;
rt = Brn;
staId = 41320;
staNm = Belmont;
stpDe = "Service toward Kimball or Linden";
stpId = 30257;
trDr = 1;
},
{
arrT = "2017-05-15T23:27:16";
destNm = Loop;
destSt = 30249;
flags = "<null>";
heading = 89;
isApp = 0;
isDly = 0;
isFlt = 0;
isSch = 0;
lat = "41.96641";
lon = "-87.67864";
prdt = "2017-05-15T23:18:16";
rn = 427;
rt = Brn;
staId = 41320;
staNm = Belmont;
stpDe = "Service toward Loop";
stpId = 30258;
trDr = 5;
},
{
arrT = "2017-05-15T23:28:25";
destNm = "95th/Dan Ryan";
destSt = 30089;
flags = "<null>";
heading = 178;
isApp = 0;
isDly = 0;
isFlt = 0;
isSch = 0;
lat = "41.97345";
lon = "-87.65853";
prdt = "2017-05-15T23:18:25";
rn = 939;
rt = Red;
staId = 41320;
staNm = Belmont;
stpDe = "Service toward 95th/Dan Ryan";
stpId = 30256;
trDr = 5;
},
{
arrT = "2017-05-15T23:32:01";
destNm = Howard;
destSt = 30173;
flags = "<null>";
heading = 357;
isApp = 0;
isDly = 0;
isFlt = 0;
isSch = 0;
lat = "41.87815";
lon = "-87.6276";
prdt = "2017-05-15T23:18:01";
rn = 928;
rt = Red;
staId = 41320;
staNm = Belmont;
stpDe = "Service toward Howard";
stpId = 30255;
trDr = 1;
},
{
arrT = "2017-05-15T23:37:32";
destNm = "95th/Dan Ryan";
destSt = 30089;
flags = "<null>";
heading = 162;
isApp = 0;
isDly = 0;
isFlt = 0;
isSch = 0;
lat = "42.00566";
lon = "-87.66565";
prdt = "2017-05-15T23:18:32";
rn = 926;
rt = Red;
staId = 41320;
staNm = Belmont;
stpDe = "Service toward 95th/Dan Ryan";
stpId = 30256;
trDr = 5;
},
{
arrT = "2017-05-15T23:40:41";
destNm = Kimball;
destSt = 0;
flags = "<null>";
heading = "<null>";
isApp = 0;
isDly = 0;
isFlt = 0;
isSch = 1;
lat = "<null>";
lon = "<null>";
prdt = "2017-05-15T23:18:41";
rn = 431;
rt = Brn;
staId = 41320;
staNm = Belmont;
stpDe = "Service toward Kimball or Linden";
stpId = 30257;
trDr = 1;
},
{
arrT = "2017-05-15T23:40:41";
destNm = "See train";
destSt = 0;
flags = "<null>";
heading = "<null>";
isApp = 0;
isDly = 0;
isFlt = 0;
isSch = 1;
lat = "<null>";
lon = "<null>";
prdt = "2017-05-15T23:18:41";
rn = 434;
rt = Brn;
staId = 41320;
staNm = Belmont;
stpDe = "Service toward Loop";
stpId = 30258;
trDr = 5;
}
);
tmst = "2017-05-15T23:18:54";
};
}
私はちょうどSwift 3
でJSON
を解析まわりで私の頭を取得しようと、まだ特定の目標を持っていません。だから、私がこの場合にしようとしているのは、各ηのステーション名(JSON内のstaNm)を取得することなので、基本的に私はBelmontをレスポンスとして取得しようとしています。私は迅速なコンパイラエラーを取得できません '[任意]型の値に' String '型のインデックスを添字できません。私はηから各キーの値を取得する方法について固まっています。どのように私はこれを行うだろう誰も考えている? CTA列車トラッカーAPIのドキュメントはここにある:http://www.transitchicago.com/developers/ttdocs/default.aspx
みんなありがとうを使用して
staNm
を取得します!これらすべての答えが助けられました。 – moesyzlack23