ios - How to parse a api for swift 3? -


have been researching on parsing quite bit. plethora of information avilable json nothing seems explain how in sensible way extract information swift 3.

this got far

func getbookdetails() {       let scripturl = "https://www.googleapis.com/books/v1/volumes?q=isbn:9781451648546" .      let myurl = url(string:scripturl)     let request = nsmutableurlrequest(url: myurl!)     request.httpmethod = "get"      let task = urlsession.shared.datatask(with: myurl! ) { (data, response, error) in         if error != nil{         print("this error",error!)             return         } else{             if let mydata = data{         do{             let myjson = try (jsonserialization.jsonobject(with: mydata, options: jsonserialization.readingoptions.mutablecontainers)) anyobject             //   print("this  json",myjson) ---> prints out json                if let dictonary = myjson["items"] anyobject? {             print("the dictonary",dictonary) // ----> output                 if let dictonaryaa = dictonary["accessinfo"] anyobject? {                     print("the accessinfo",dictonaryaa)                 }              }           } catch{             print("this in catch")                 }             }      //data         }     }     task.resume()  }  }      output :         dictonary (     {     accessinfo =         {         accessviewstatus = sample;         country = us;       =============    relevant data in https://www.googleapis.com/books/v1/volumes?   q=isbn:9781451648546"     ==========================         title = "steve jobs";     }; } ) 

just need parse through json data name, author , title of book reference isbn. know there should better way things understandable new language

first of all, json types value types in swift 3 unspecified type any, not anyobject.

second of all, there 2 collection types in json type set, dictionary ([string:any]) , array ([any], in cases [[string:any]]). it's never any nor anyobject.

third of all, given json not contain key name.


for convenience let's use type alias json dictionary:

typealias jsondictionary = [string:any] 

the root object dictionary, in dictionary there array of dictionaries key items. , pass no options, .mutablecontainers nonsense in swift.

guard let myjson = try jsonserialization.jsonobject(with: mydata) as? jsondictionary,        let items = myjson["items"] as? [jsondictionary] else { return } 

iterate through array , extract values title , authors array way. both values in dictionary key volumeinfo.

for item in items {      if let volumeinfo = item["volumeinfo"] as? jsondictionary {          let title = volumeinfo["title"] as? string          let authors = volumeinfo["authors"] as? [string]          print(title ?? "no title", authors ?? "no authors") 

the isbn information in array key industryidentifiers

         if let industryidentifiers = volumeinfo["industryidentifiers"]  as? [jsondictionary] {              identifier in industryidentifiers {                 let type = identifier["type"] as! string                 let isbn = identifier["identifier"] as! string                 print(type, isbn)              }          }      } } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -