gpt4 book ai didi

json - 快速下载和解析 json

转载 作者:IT老高 更新时间:2023-10-28 12:47:11 26 4
gpt4 key购买 nike

我正在尝试从网站获取 JSON 并在将其放入 iOS View 之前对其进行解析。

这是我的代码;

func startConnection(){
let urlPath: String = "http://binaenaleyh.net/dusor/"
var url: NSURL = NSURL(string: urlPath)
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
connection.start()
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}

func buttonAction(sender: UIButton!){
startConnection()
}

func connectionDidFinishLoading(connection: NSURLConnection!) {
var err: NSError
// throwing an error on the line below (can't figure out where the error message is)
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
}

这是 JSON 的链接;

http://binaenaleyh.net/dusor/

我在这里做错了什么?

最佳答案

这两个功能对我有用:

    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

return boardsDictionary
}

关于json - 快速下载和解析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24065536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com