我试图从Zoopla API中获取一些JSON值,但没有像其他API那样响应,很可能是我的错,我看不到错误,有人可以检查我的代码并找出是否缺少什么?

问候,

import UIKit

class ViewController: UIViewController {

    @IBOutlet var areaLabel : UILabel!
    @IBOutlet var streetLabel : UILabel!
    @IBOutlet var townLabel : UILabel!
    @IBOutlet var postcodeLabel : UILabel!
    @IBOutlet var outputTypeLabel : UILabel!
    @IBOutlet var posterImageView : UIImageView!
    @IBOutlet var completar : UITextField!



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonPressed (sender: UIButton)
    {
        self.searchZoopla("\(completar)")
    }

    func searchZoopla (forContent : String) {
        var spacelessString = forContent.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
        var urlPath = NSURL(string: "http://api.zoopla.co.uk/api/v1/zed_indices?postcode=\(spacelessString)&output_type=outcode&api_key=<api_key>")

        var session = NSURLSession.sharedSession()

        var task = session.dataTaskWithURL(urlPath) {
            data, response, error -> Void in

            if ((error) != nil) {
                println(error.localizedDescription)
            }

            var jsonError : NSError?

            var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as Dictionary<String, String>

            if(jsonError != nil) {
                println(error.localizedDescription)
            }


            self.areaLabel.text = jsonResult["area_name"]
            self.streetLabel.text = jsonResult["street"]
            self.townLabel.text = jsonResult["town"]
            self.postcodeLabel.text = jsonResult["postcode"]
            self.outputTypeLabel.text = jsonResult ["area_url"]
        }

    }

}

最佳答案

如果您要从zoopla进行JSON响应,则在URL中输入,请通过.js指定它,否则响应将在xml中。因此您的网址将为http://api.zoopla.co.uk/api/v1/zed_indices.js?postcode=...

zoopla中默认的默认输出格式将是XML,可以使用文件扩展名请求其他输出格式,例如,使用“ .js”后缀将产生JavaScript输出。

10-08 14:36