Zoopla API,尝试在swift中从JSON获取一些值

时间:2014-09-19 11:39:43

标签: ios json api swift getjson

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

此致

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"]
        }

    }

}

1 个答案:

答案 0 :(得分:0)

如果您想要来自zoopla的JSON响应,请在您的网址中输入.js,否则响应将在xml中。因此,您的网址为http://api.zoopla.co.uk/api/v1/zed_indices.js?postcode=...

zoopla中的默认假定输出格式是XML,其他可以使用文件扩展名请求,例如使用&#34; .js&#34;后缀会产生JavaScript输出。