ios - How to Parse JSON with more than one array in Swift -


i'm new in swift language, newbie question have ask cause mind little bit confused dictionary , array usage here, got json this;

 {   "mainpagelast": [   {},   {}..   ],   "mainpagesport": [   {},   {}..   ],   "mainpageeco": [   {},   {}..   ],   "mainpagepol": [   {},   {}..   ] } 

i made base class includes of these arrays dictionary this;

public class func modelsfromdictionaryarray(array:nsarray) -> [json4swift_base]     {         var models:[json4swift_base] = []         item in array         {             models.append(json4swift_base(dictionary: item as! nsdictionary)!)         }         return models     }  required public init?(dictionary: nsdictionary) {          if (dictionary["mainpagelast"] != nil) { mainpagelast = mainpagelast.modelsfromdictionaryarray(dictionary["mainpagelast"] as! nsarray) }         if (dictionary["mainpagesport"] != nil) { mainpagesport = mainpagesport.modelsfromdictionaryarray(dictionary["mainpagesport"] as! nsarray) }         if (dictionary["mainpageeco"] != nil) { mainpageeco = mainpageeco.modelsfromdictionaryarray(dictionary["mainpageeco"] as! nsarray) }         if (dictionary["mainpagepol"] != nil) { mainpagepol = mainpagepol.modelsfromdictionaryarray(dictionary["mainpagepol"] as! nsarray) }     }  public func dictionaryrepresentation() -> nsdictionary {          let dictionary = nsmutabledictionary()           return dictionary     } 

and try data , try parse , want see returning data in debug screen;

func loadheadline(completion : ((anyobject) -> void)!) {          let urlstring = "http://myapiurl."          let session = nsurlsession.sharedsession()         let newsurl = nsurl(string: urlstring)          let task = session.datataskwithurl(newsurl!, completionhandler: {             (data, response, error) -> void in              {                 let jsondata = try nsjsonserialization.jsonobjectwithdata(data!, options: .mutablecontainers) as! dictionary<string, anyobject>                  var models:[json4swift_base] = []                 item in jsondata                 {                     models.append(json4swift_base(dictionary: item nsdictionary)!)                 }              } catch let error nserror{                 print("failed load: \(error.localizeddescription)")             }          })         task.resume()     } 

i know need reach each [0], [1]... items 1 one in json4swift_base class not figure how to.

this exception throws when try

models.append  exc_bad_instruction (code=exc_i386_invop subcode=0x0) 

now, way should follow or , put data in one.

thanks in advice..

let jsondata = try nsdata(contentsoffile: path, options: nsdatareadingoptions.datareadingmappedifsafe)                 {                     let jsonresult = try nsjsonserialization.jsonobjectwithdata(jsondata, options: nsjsonreadingoptions.mutablecontainers) as! nsarray                      if let alldata = jsonresult[0] as? nsdictionary {                         let header = alldata.objectforkey("mainpagemansetdata")                         let magazine = alldata.objectforkey("mainpagemagazinedata")                         let sports = alldata.objectforkey("mainpagesportsdata")                         let articles = alldata.objectforkey("mainpageotherarticles")                          print("\(header) \n \(magazine) \n \(sports) \n \(articles)")                     }                 } catch {} 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -