swift - how to parse this kind of JSON API by using SwiftyJSON -


a project using swift , parse json api using library called swiftyjson

[{"a":1, "b":"2", "c":"3", "d":"4" }, {"a2":12, "b3":"23", "c4":"34", "d5":"45" }]

↑this api , code↓

import uikit import swiftyjson  class viewcontroller: uiviewcontroller{   override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib.  parsejson() }  func parsejson(){  let path:string=nsbundle.mainbundle().pathforresource("jsonfile", oftype: "json")as string! let jsondata=nsdata(contentsoffile: path) nsdata! let readablejson=json(data:jsondata, options:nsjsonreadingoptions.mutablecontainers, error:nil)  var number=readablejson["a"]  nslog("\(number)") }  override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. }  } 

somethings wrong , api’s method not dictionary.

i have parse json anyway can tell me way parse api it's method array?

if have other way or alternative's project please teach me.

thanks cooperation

the outer object of json array ( [] -> array, {} -> dictionary). requested dictionary first element of array.

let path = nsbundle.mainbundle().pathforresource("jsonfile", oftype: "json")! let jsondata = nsdata(contentsoffile: path)! let readablejson = json(data:jsondata, options:nsjsonreadingoptions.mutablecontainers, error:nil)  if let number = readablejson[0].dictionary?["a"] {   println(number) } 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -