이 JSON URL ( https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y ) 에서 "이벤트", "Hasta"및 "위치"를 추출하고 싶지만 수행 방법에 어려움을 겪고 있습니다. 그것? 누구든지 나를 도울 수 있습니까? 여기에 내 코드가 있습니다 ... 그런 다음이 3으로 tableview를 채우고 싶습니다.
override func viewDidLoad() {
super.viewDidLoad()
splitViewController!.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible
UINavigationBar.appearance().barTintColor = UIColor(red: 52.0/255.0, green: 170.0/255.0, blue: 220.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
let url = NSURL(string:"https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y")!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url) { (data, response, error) -> Void in
if error != nil {
print(error)
} else {
if let data = data {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
if jsonResult!.count > 0 {
if let results = jsonResult!["results"] as? NSDictionary, collection2 = results["collection2"] as? NSArray {
for entry in collection2 {
if let dict = entry["Event"] as? NSDictionary {
print(dict)
}
else if let array = entry as? NSArray {
} else {
}
}
if let items = jsonResult?["Date"] as? NSArray {
print(items)
}
}
}
} catch {
print("In catch block")
}
}
}
}
task.resume()
}
Swift로 JSON을 파싱하는 것은 지옥입니다. SwiftyJSON으로 쉽게 할 수 있습니다 .
JSON으로 :
// Get content of json url
let jsonString = try NSString.init(contentsOfURL: url!, encoding: NSUTF8StringEncoding)
// Create JSON object from data
let json = JSON(data: jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)
// Check if array for key "collection2" exists
if let collection2 = json["results"]["collection2"].array {
// Create JSON array from it and loop for each object
for (key, subJson):(String, JSON) in JSON(collection2) {
// Check if dictionary for key "Event" exists
if let event = subJson["Event"].dictionary {
print(event)
}
// Check if string for key "Hasta" exists
if let hasta = subJson["Hasta"].string {
print(hasta)
}
// Check if string for key "Location" exists
if let location = subJson["Location"].string {
print(location)
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다