1L30
1L30

Reputation: 259

Get JSON in Swift

This is my code to get JSON, and it's work with this url I found on an other questions : http://binaenaleyh.net/dusor/. But, when I use it with this url : http://www.netcampus.fr/api/schools, it didn't work at all. I have an error who said : "exc_breakpoint (code=exc_i386_bpt subcode=0x0)"

Is my code wrong, or is it the JSON data ?

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    var myData:NSData = getJSON("http://www.netcampus.fr/api/schools")
    println(myData) // show me data
    var myDict:NSDictionary = parseJSON(myData)
    println(myDict)
}

func getJSON(urlToRequest: String) -> NSData{
    return NSData(contentsOfURL: NSURL(string: urlToRequest))
}

func parseJSON(inputData: NSData) -> NSDictionary{
    var error: NSError?
    var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary // error here
    return boardsDictionary
}
}

Upvotes: 3

Views: 1697

Answers (3)

Sagar vaishnav
Sagar vaishnav

Reputation: 97

    let urlString = "webAPI"
    var request = URLRequest(url: URL(string: urlString)!)

    let session = URLSession.shared
    request.httpMethod = "GET"
    session.dataTask(with: request)

    {
        data, response, error in
        if error != nil
        {
            print(error!.localizedDescription)
            return
        }

        do
        {


            let jsonResult: NSDictionary? = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
            self.dict = jsonResult as! NSMutableDictionary
            print("Synchronous\(jsonResult)")

            DispatchQueue.main.async
                {

                let url = URL(string: ((((self.dict.value(forKey: "sources") as! NSArray).object(at: 5) as! NSDictionary).value(forKey: "urlsToLogos") as? NSDictionary)?.value(forKey: "small") as? String)!)
                let data = try? Data(contentsOf: url!)


            }


                        }
        catch
        {
            print"error"
        }

        }.resume()

Upvotes: -1

karthikPrabhu Alagu
karthikPrabhu Alagu

Reputation: 3401

As @reecon pointed out ,your code should be like this

//JSON Parsing
func JSONParsingSample() {

    var myData:NSData = getJSON("http://www.netcampus.fr/api/schools")
    //println(myData) // show me data
    var myDict:NSArray = parseJSON(myData)
    println(myDict)
}
func getJSON(urlToRequest: String) -> NSData{
    return NSData(contentsOfURL: NSURL(string: urlToRequest))
}

func parseJSON(inputData: NSData) -> NSArray{
    var error: NSError?
    var boardsDictionary: NSArray = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSArray 
    return boardsDictionary
}
//end

Upvotes: 3

Rafał Sroka
Rafał Sroka

Reputation: 40030

Your parseJSON method crashes when parsing the second JSON. NSJSONSerialization maps its contents to an array and you are expecting a dictionary:

var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary // error here

Upvotes: 6

Related Questions