当您继续获取EXC_BREAKPOINT(code = EXC_I386_BPT,subcode = 0x0)错误时,如何解析Swift中的JSON?

问题描述:

我试图访问GitHub的统计数据,并作为当您继续获取EXC_BREAKPOINT(code = EXC_I386_BPT,subcode = 0x0)错误时,如何解析Swift中的JSON?

[ 
{"name" : "Peter"}, 
{ "name" : "Daniel"}, 
] 

每次我试图解析这一点,我得到一个EXC_BREAKPOINT(代码= EXC_I386_BPT,子码=为0x0)错误被返回从API的信息。 有没有一种简单的方法来解析这个使用Swift?

+0

我USI用一个JSON解析方法来解析他们以前的API调用,但是当我试图解析上述代码时,我收到了解析错误。如果我能将这些信息放入数组中,我认为这将是一帆风顺的。然而,我从来没有见过这种语法。 – DosCadenas 2014-10-04 23:50:58

+0

那么,你的问题是'='应该是JSON中的':'。只需用':'替换'='并使用JSON解析:http://www.topcoder.com/blog/calling-apis-parsing-json-with-swift/ – plalx 2014-10-04 23:51:41

+0

这是我的问题。 []不是JSON,对吗? – DosCadenas 2014-10-04 23:53:56

试图解析当一个更传统的方法,我尝试使用:

//Set up the network request, asynchronously 
    let urlPath: String = "https://api.github.com/users/" + userName 
    var url: NSURL = NSURL(string: urlPath) 
    var request: NSURLRequest = NSURLRequest(URL: url) 
    let queue:NSOperationQueue = NSOperationQueue() 

    //Make the asynchronous request 
    NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in 

     var err: NSError 


     //Store the JSON data from the Github api 
     var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary 

这给了我一个EXC_BREAKPOINT(代码= EXC_I386_BPT,子码=为0x0)错误

所以,我尝试另一种分析方法

var url: NSURL = NSURL(string: urlString) 
    var request: NSURLRequest = NSURLRequest(URL: url) 
    let queue: NSOperationQueue = NSOperationQueue() 


    //Store the JSON data from the Github api 
    var jsonResult: NSData = NSData(contentsOfURL: url) 

    var error:NSError? 

    // Retrieve Data 
    var JSONData = NSData.dataWithContentsOfURL(url, options: NSDataReadingOptions(), error: &error) 
    // Create another error optional 
    var jsonerror:NSError? 
    // We don't know the type of object we'll receive back so use AnyObject 
    let swiftObject:AnyObject = NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.AllowFragments, error:&jsonerror)! 
    // JSONObjectWithData returns AnyObject so the first thing to do is to downcast this to a known type 
    if let nsDictionaryObject = swiftObject as? NSDictionary { 
     if let swiftDictionary = nsDictionaryObject as Dictionary? { 
      println(swiftDictionary) 
     } 
    } 
    else if let nsArrayObject = swiftObject as? NSArray { 
     if let swiftArray = nsArrayObject as Array? { 
      println(swiftArray) 
     } 
    } 

这种方法让我来分析信息