0

我正在尝试使用 Opendata 制作应用程序。这是我的 JSON 数据:http ://datatank.stad.gent/4/infrastructuur/publieksanitair

我遇到的问题是我不知道如何解析这个 JSON 文件。我总是收到“数据不包含根对象”。

所以它在 Service.swift 文件中出错了。我确定我的请求有效,因为在调试时我看到返回了数据,但我不知道如何处理它。

您可以从以下位置拉出我的项目:https ://github.com/StijnPil/iOSProjectShared/tree/develop

但我也在下面放了重要的代码:

服务.swift

import Foundation

class Service
{
enum Error: ErrorType
{
    case InvalidJsonData(message: String?)
    case MissingJsonProperty(name: String)
    case MissingResponseData
    case NetworkError(message: String?)
    case UnexpectedStatusCode(code: Int)
}

static let sharedService = Service()

private let url: NSURL
private let session: NSURLSession

private init() {
    let path = NSBundle.mainBundle().pathForResource("Properties", ofType: "plist")!
    let properties = NSDictionary(contentsOfFile: path)!
    url = NSURL(string: properties["baseUrl"] as! String)!
    session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
}

func createFetchTask(completionHandler: Result<[PubliekSanitair]> -> Void) -> NSURLSessionTask {
    return session.dataTaskWithURL(url) {
        data, response, error in
        let completionHandler: Result<[PubliekSanitair]> -> Void = {
            result in
            dispatch_async(dispatch_get_main_queue()) {
                completionHandler(result)
            }
        }
        guard let response = response as? NSHTTPURLResponse else {
            completionHandler(.Failure(.NetworkError(message: error?.description)))
            return
        }
        guard response.statusCode == 200 else {
            completionHandler(.Failure(.UnexpectedStatusCode(code: response.statusCode)))
            return
        }
        guard let data = data else {
            completionHandler(.Failure(.MissingResponseData))
            return
        }


        do {
            guard let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as? [NSDictionary] else {
                completionHandler(.Failure(.InvalidJsonData(message: "Data does not contain a root object.")))
                return
            }

            //old code
            //let lots = try json.filter { $0["city"]?["name"] as? String == "Gent" }.map { try ParkingLot(json: $0) }

            //new code
            let lots = try json.map{ try PubliekSanitair(json: $0)}
            completionHandler(.Success(lots))
        } catch let error as Error {
            completionHandler(.Failure(error))
        } catch let error as NSError {
            completionHandler(.Failure(.InvalidJsonData(message: error.description)))
        }
    }
}
}

结果.swift

enum Result<T>
{
    case Success(T)
    case Failure(Service.Error)
}

PubliekSanitair.swift

import Foundation

class PubliekSanitair
{

let type_sanit: String

init(type_sanit: String){
    self.type_sanit = type_sanit
}
}



extension PubliekSanitair
{
convenience init(json: NSDictionary) throws {
    guard let document = json["Document"] as? NSDictionary else{
        throw Service.Error.MissingJsonProperty(name: "Document")
    }
    guard let folder = document["Folder"] as? NSDictionary else{
        throw Service.Error.MissingJsonProperty(name: "Folder")
    }
    guard let placemark = folder["Placemark"] as? NSDictionary else{
        throw Service.Error.MissingJsonProperty(name: "Placemark")
    }

    guard let extendedData = placemark["ExtendedData"] as? NSDictionary else{
        throw Service.Error.MissingJsonProperty(name: "Placemark")
    }
    guard let schemaData = extendedData["SchemaData"] as? NSDictionary else{
        throw Service.Error.MissingJsonProperty(name: "Placemark")
    }
    guard let simpleData = schemaData["SimpleData"] as? NSDictionary else{
        throw Service.Error.MissingJsonProperty(name: "Placemark")
    }

    guard let type_sanit = simpleData[0]!["@text"] as? String else{
        throw Service.Error.MissingJsonProperty(name: "@text in type_sanit")
    }

    self.init(type_sanit: type_sanit)
      }
}
4

0 回答 0