Parsing JSON array in swift

前端 未结 2 1238
星月不相逢
星月不相逢 2020-12-21 03:26

I am trying to parse the following JSON

[
    {
        \"id\": \"5\",
        \"name\": \"Test\",
        \"team1\": \"thingy team\",
        \"team2\": \"c         


        
相关标签:
2条回答
  • 2020-12-21 04:09

    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()
    
    0 讨论(0)
  • 2020-12-21 04:12

    From the CFNetworking source code I see -1005 referring to kCFURLErrorNetworkConnectionLost = -1005. So something is wrong with your connection.

    0 讨论(0)
提交回复
热议问题