Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

ios - Parsing JSON array in swift

I am trying to parse the following JSON

[
    {
        "id": "5",
        "name": "Test",
        "team1": "thingy team",
        "team2": "clicky team",
        "category": "4",
        "end_date": "1415217600",
        "cat_name": "new thingy",
        "team1_bets": 1,
        "team2_bets": 1
    }
]

This is the JSON I am getting from my web services, and I am using the following code to parse it:

let urlAsString = "http://codespikestudios.com/betting_app/bet/get_events/4"
    //let urlAsString = "http://api.topcoder.com/v2/challenges?pageSize=2"
    let url: NSURL  = NSURL(string: urlAsString)!
    let urlSession = NSURLSession.sharedSession()

    let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
        if (error != nil) {
            println(error.localizedDescription)
        }
        var err: NSError?

        var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
        if (err != nil) {
            println("JSON Error (err!.localizedDescription)")
        }

        println(jsonTime)
    })
    jsonQuery.resume()

I am getting the following error

The operation couldn’t be completed. (NSURLErrorDomain error -1005.) fatal error: unexpectedly found nil while unwrapping an Optional value

how can I solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your URL is returning the following JSON -

[
    {
        "id": "5",
        "name": "Test",
        "team1": "thingy team",
        "team2": "clicky team",
        "category": "4",
        "end_date": "1415217600",
        "cat_name": "new thingy",
        "team1_bets": 1,
        "team2_bets": 1
    }
]

The outermost square brackets indicate that the root object is an array, so attempting to cast the result of your JSON parse to an NSDictionary causes problems.

Your code should be -

let urlAsString = "http://codespikestudios.com/betting_app/bet/get_events/4"
let url: NSURL  = NSURL(string: urlAsString)!
let urlSession = NSURLSession.sharedSession()

let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
    if (error != nil) {
        println(error.localizedDescription)
    }
    var err: NSError?

    var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSArray?
    if (err != nil) {
        println("JSON Error (err!.localizedDescription)")
    }

    println(jsonResult!)
})
jsonQuery.resume()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...