CTA 기차 도착 JSON을 구문 분석하려고합니다. 에서JSON Swift 3 CTA Train Tracker 구문 분석
{
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";
};
}
내 ViewController.swift
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.
}
}
난 그냥 Swift 3
에 JSON
을 구문 분석 주위에 내 머리를 얻으려고 노력, 아직 특정 목표를 가지고하지 않습니다. 그래서이 경우에 내가하려고하는 것은 각 eta에 대한 스테이션 이름 (JSON의 staNm)을 얻는 것이므로 기본적으로 Belmont를 응답으로 사용하려고합니다. 신속한 컴파일러 오류가 발생합니다. '유형'의 값에 'Any'유형의 인덱스를 'String'유형의 인덱스와 함께 사용할 수 없습니다. 나는 각 키에 대한 값을 eta에서 얻는 방법에 매달렸다. 아무도 내가 어떻게 할 수 있을지 전혀 모르겠다. CTA를 기차 추적기 API에 대한 문서는 여기에 있습니다 : http://www.transitchicago.com/developers/ttdocs/default.aspx
모두에게 감사드립니다! 이 모든 대답들이 도움이되었습니다. – moesyzlack23