JSON Swift TableView 구문 분석

돈 드레이퍼

이 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()
}
Kientux

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Swift로 Json 구문 분석

분류에서Dev

JSON Swift 구문 분석

분류에서Dev

Swift JSON 구문 분석

분류에서Dev

Swift Alamofire JSON 구문 분석

분류에서Dev

Swift에서 Json 구문 분석

분류에서Dev

잘못된 JSON Swift 5 구문 분석

분류에서Dev

Swift의 JSON 구문 분석 오류

분류에서Dev

AFNetworking에서 JSON 구문 분석-Swift

분류에서Dev

Swift의 OpenWeatherMap에서 JSON 구문 분석

분류에서Dev

JSON Swift 첨자 오류 구문 분석

분류에서Dev

Swift 2.0의 JSON 구문 분석 오류

분류에서Dev

Swift 2 구문 분석 및 JSON 읽기

분류에서Dev

JSON을 TableView xcode 6.0.1, iOS8로 구문 분석

분류에서Dev

문자열에서 구문 분석 된 json으로 내 tableview 채우기

분류에서Dev

SWIFT 구문 분석 문제

분류에서Dev

Swift로 JSON 문자열 구문 분석 오류

분류에서Dev

Alamofire, Swift 2.0, SwiftyJSON : 응답 본문을 JSON으로 구문 분석

분류에서Dev

Swift 1.2에서 JSON 구문 분석 문제

분류에서Dev

JSON SWIFT 3 단일 문자열 Yahoo API로 구문 분석

분류에서Dev

API에서 Swift로 JSON 데이터 구문 분석 문제

분류에서Dev

Json Object Swift 5 내에서 Json 객체 구문 분석

분류에서Dev

JSON에 대한 Swift JSON 동적 키 구문 분석

분류에서Dev

JSON 구문 분석 문제

분류에서Dev

JSON 구문 분석 문제

분류에서Dev

tableview NSObject json 구문 분석에 "추가로드"를 추가합니다.

분류에서Dev

Swift에서 동적 선택적 키-값 JSON 구문 분석

분류에서Dev

Swift에서 JSON 배열의 구문 분석 시도

분류에서Dev

Swift를 사용하여 JSON 구문 분석

분류에서Dev

Swift에서 Alamofire로 JSON 응답 구문 분석