0

I am trying to receive data from a JSON URL and then put that data into a UITableView - I am currently trying to put it into a UIlabel for simplicity but will try and figure how to use a table later!

I am using the latest version of Swift and xCode, SwiftyJSON and Alamofire.

There is no errors in my code, yet the thing that I want to happen is not happening. My UILabel is hooked up fine as well!

Here is my code - (For some reason when I paste my code onto here some of the code is not listed as code - it is in my code though so that is not the issue!!!)

import UIKit
import Alamofire

class ViewController: UIViewController {

    @IBOutlet weak var dataContainer: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = "https://opendata.gov.je/dataset/bdae149d-ff02-443a-b5ed-446a206e74f7/resource/042ea501-a4eb-48c1-9a23-9f01084d72e6/download/stats-from-govmetric.json.txt"

        Alamofire.request(.GET, url).validate().responseJSON { response in
            switch response.result {
            case .Success(let data):
                let json = JSON(data)
                let name = json["name"].stringValue
                self.dataContainer.text = data as? String
                print(response.request)
                print(response.response)
                print(response.result)
                print(name)
            case .Failure(let error):
                print("Request failed with error: \(error)")
            }
        }
    }
}
Jay Patel
  • 2,642
  • 2
  • 18
  • 40
doodle911
  • 55
  • 2
  • 9

1 Answers1

0

Never use the SwiftyJSON, but looks like you parse the json not right way.

self.dataContainer.text = data as? String

You can not do this, because data 100% non a String (i think it's NSData). So you need do something like this: self.dataContainer.text = name. But it doesn't work too, because you receive json as array, not object. So, i think, this code will work:

self.dataContainer.text = json[0]["Date range"].string
pacification
  • 5,838
  • 4
  • 29
  • 51
  • Thank you very much, I am quite new to Swift! – doodle911 Jul 01 '16 at 19:42
  • I changed the link/URL to http://www.gov.je/_layouts/15/C5.Gov.Je.CarParks/proxy.aspx and have edited the code to suit that change but the same incident occurs. When I first put it in, I had to do this - http://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http I have changed ["Date Range"] to ["Green Street"] Do you have any idea why this might be occurring again, sorry of if it is blatantly obvious. – doodle911 Jul 02 '16 at 14:39
  • I fell it might be because the new link's JSON is not contained in an array but I am unsure on how to fix it as I would like to get a specific piece of data - "Green Street" – doodle911 Jul 02 '16 at 14:50
  • @doodle911 about your new url. you can access some info like this `json["carparkData"]["Jersey"]["carpark"][0]["name"].string`. This should get you the `"Green Street"` string. By the way, i recommend you to learn about json to understand what going on ;) – pacification Jul 02 '16 at 17:48
  • 1
    Thank you so much! I understood how to JSON was layered and stuff, I was just unsure about the swift/xCode side of things. Although I am quite new to both. Thank you so much! :) – doodle911 Jul 02 '16 at 19:45