I'm trying to get the JSON from a website and parse it before putting it inside of an iOS view.

Here's my code;

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
    }

And this is the link for the 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相关问答推荐

在Vega中如何通过滑块改变条形图的宽度

组合不同属性的Jolt Spec

使用JQ将对象数组转换为平面数组

将JSON数组组织到菜单中

Jolt 变换以展平 json 字符串数组

jq :遍历 json 并在键存在时检索集合

如何修复通过在 tsconfig.json 文件中添加allowImportingTsExtensions引发的错误 TS5023?

如何加入或合并列表元素列表(未知长度)

Shell脚本空格转义

Servicestack 返回数组而不是带有数组的对象

Jolt - 在同一级别添加时组合值的问题

UTF-8 字符编码之战 json_encode()

JSON Schema 与 XML Schema 的比较及其future

在 JSON 字符串中react i18n 换行符

JSON 语法错误:'unexpected number' 或 'JSON.parse: expected ',' or '}' after property value in object'

将 javascript 对象或数组转换为 json 以获取 ajax 数据

Peewee 模型转 JSON

有什么方法可以判断您安装的 gulp 版本吗?

Python 到 JSON 序列化在十进制上失败

Microsoft.Net.Http 与 Microsoft.AspNet.WebApi.Client