2016-07-23 6 views
-1

Schleife Ich lerne schnell und objektiv-c. Ich möchte den "show_name" in meinem Json für alle Shows extrahieren.Ich kann nicht json Objekt auf schnelle xcode

Das ist mein json:

{ 
"data": { 
    "12 Monkeys": { 
     "air_by_date": 0, 
     "anime": 0, 
     "cache": { 
      "banner": 1, 
      "poster": 1 
     }, 
     "indexerid": 272644, 
     "language": "en", 
     "network": "Syfy", 
     "next_ep_airdate": "", 
     "paused": 0, 
     "quality": "SD", 
     "show_name": "12 Monkeys", 
     "sports": 0, 
     "status": "Continuing", 
     "subtitles": 1, 
     "tvdbid": 272644 
    }, 
    "2 Broke Girls": { 
     "air_by_date": 0, 
     "anime": 0, 
     "cache": { 
      "banner": 1, 
      "poster": 1 
     }, 
     "indexerid": 248741, 
     "language": "en", 
     "network": "CBS", 
     "next_ep_airdate": "2016-10-10", 
     "paused": 0, 
     "quality": "SD", 
     "show_name": "2 Broke Girls", 
     "sports": 0, 
     "status": "Continuing", 
     "subtitles": 0, 
     "tvdbid": 248741 
    }, 
    "American Horror Story": { 
     "air_by_date": 0, 
     "anime": 0, 
     "cache": { 
      "banner": 1, 
      "poster": 1 
     }, 
     "indexerid": 250487, 
     "language": "en", 
     "network": "FX (US)", 
     "next_ep_airdate": "2016-09-14", 
     "paused": 0, 
     "quality": "SD", 
     "show_name": "American Horror Story", 
     "sports": 0, 
     "status": "Continuing", 
     "subtitles": 0, 
     "tvdbid": 250487 
    }, 

Dies ist mein Code und ich verwende swiftyJson:

class ViewController: UIViewController { 


    let baseURL = "http://xxx.xxx.xxx.xxx:8083/api/api/?cmd=shows&sort=name&paused=0" 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    getJSON() 
} 

func getJSON(){ 
    let url = NSURL(string: baseURL) 
    let request = NSURLRequest(URL: url!) 
    let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) 
    let task = session.dataTaskWithRequest(request) 
    { (data, response, error) -> Void in 

     if error == nil { 

     let swiftyJSON = JSON(data: data!) 
     print(swiftyJSON) 
     let title = swiftyJSON["data"]["Sense8"]["show_name"] 
     print(title) 



    }else { 
    print("there was an error") 

    } 

    } 
    task.resume() 
} 

}

Antwort

0

Ihre swiftyJSON ist ein Array von JSON, (dh [JSON]), Daher können Sie nicht per Index darauf zugreifen. Sie können es wie folgt analysieren:

if let dataArray = swiftyJSON.array{ 
     for json in dataArray { 

      // data information 

      let showName = json["show_name"].stringValue 
      let status = json["status"].stringValue 
      let subtitles = json["subtitles"].stringValue 
      print(showName) 
      print(status) 
      print(subtitles) 
      } 
} 
Verwandte Themen