Swift基础--解析服务器Json内容

页面导航:首页 > 网络编程 > JavaScript > Swift基础--解析服务器Json内容

Swift基础--解析服务器Json内容

来源: 作者: 时间:2016-02-20 09:49 【

ViewController swift JieParseJson Created by jiezhang on 14-10-5 Copyright (c) 2014年 jiezhang All rights reserved import UIKitclass ViewControll
//
//  ViewController.swift
//  JieParseJson
//
//  Created by jiezhang on 14-10-5.
//  Copyright (c) 2014年 jiezhang. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
                            
    @IBOutlet weak var city: UILabel!
    @IBOutlet weak var cityid: UILabel!
    @IBOutlet weak var temp1: UILabel!
    @IBOutlet weak var temp2: UILabel!
    @IBOutlet weak var weather: UILabel!
    @IBOutlet weak var img1: UILabel!
    @IBOutlet weak var img2: UILabel!
    @IBOutlet weak var ptime: UILabel!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    func getWeather()
    {
        var url = NSURL(string: "http://www.weather.com.cn/data/cityinfo/101010100.")
        var data = NSData(contentsOfURL: url)
        var str = NSString(data: data, encoding: NSUTF8StringEncoding)
        println(str)
        var json : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil)
        let weatherinfo: AnyObject = json.objectForKey("weatherinfo")!
        city.text = weatherinfo.objectForKey("city") as String
        cityid.text = weatherinfo.objectForKey("cityid") as String
        temp1.text = weatherinfo.objectForKey("temp1") as String
        temp2.text = weatherinfo.objectForKey("temp2") as String
        weather.text = weatherinfo.objectForKey("weather") as String
        img1.text = weatherinfo.objectForKey("img1") as String
        img2.text = weatherinfo.objectForKey("img2") as String
        ptime.text = weatherinfo.objectForKey("ptime") as String
    }

    @IBAction func updateWeather(sender: AnyObject) {
        getWeather()
    }
}


当时想显示图片来,但是返回的图片是假的,我查看了页面,css里面是这样的

big.jpg80{background-image:url(../../i/weather2014/jpg/gray80.jpg);height:80px;width:80px}.on big.jpg30{background-image:url(../../i/weather2014/jpg/white30.jpg);height:30px;width:31px}.on big.jpg50{background-image:url(../../i/weather2014/jpg/blue50.jpg);height:50px;width:50px}.on big.jpg80{background-image:url(../../i/weather2014/jpg/blue80.jpg);height:80px;width:80px}.sk big.jpg80{background-image:url(../../i/weather2014/jpg/blue80.jpg);height:80px;width:80px}
所以真实地址:http://i.tq121.com.cn/i/weather2014/jpg/blue80.jpg,打开后是一组图片,像下面的,在网站上看到的都是用css切割出来的。

\







Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<