Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Swift 4解析JSON时出错_Ios_Json_Swift - Fatal编程技术网

Ios 使用Swift 4解析JSON时出错

Ios 使用Swift 4解析JSON时出错,ios,json,swift,Ios,Json,Swift,我一直在网上寻找答案,但作为Swift的法国新手,我什么也没找到。 尝试解码时,我在解析时遇到错误 这是我的密码: import UIKit struct Station: Decodable { let ID: Int let name: String let lines: String } class ViewController: UIViewController { override func viewDidLoad() { supe

我一直在网上寻找答案,但作为Swift的法国新手,我什么也没找到。 尝试解码时,我在解析时遇到错误

这是我的密码:

import UIKit

struct Station: Decodable {
    let ID: Int
    let name: String
    let lines: String
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let urlString = "https://celestinlebehot.com/saintlobus-export-2.json"
        guard let url = URL(string: urlString) else {
            return
        }
        URLSession.shared.dataTask(with: url) { (data, response, err) in
            guard let data = data else { return }


            do {
                let stations = try JSONDecoder().decode([Station].self, from: data)
                print("\(stations)")

            } catch let jsonErr {
                print("error in parsing",jsonErr)
            }
            }.resume()

    }

}
以下是我在运行应用程序时遇到的错误:

解析valueNotFound时出错(Swift.KeyedDecodingContainer,Swift.DecodingError.Context(编码路径:[Foundation.[U-JSONKey in _12768CA107A31EF2DCE034FD75B541C9](stringValue:“Index 0”,intValue:Optional(0))),调试说明:“无法获取键控解码容器--改为找到空值。”,underyingError:nil))

怎么了

  • 如果你知道如何使用JSON(包含公交站点坐标和时间表)创建公共交通数据库,我会听取建议,因为我刚刚开始使用Swift(app Store上的Saint-LôCitybus应用程序)进行编码

谢谢:)

好的,我已经创建了一个新项目,并将此代码粘贴到其中,它成功了。怎么用?我不知道。但是它成功了。

代码看起来不错,但是请检查json文件,因为错误表明您没有特定键的值。并且还将json变量设置为可选。

使用json文件时,代码运行得非常完美,这只是因为该键没有值。在这种情况下,上述解决方案肯定会起作用。代码是正确的,应该可以工作。这是真的密码吗?