Swift basics-Parse server Json content, swift-Parse json

Source: Internet
Author: User

Swift basics-Parse server Json content, swift-Parse json

//// ViewController. swift // JieParseJson /// Created by jiezhang on 14-10-5. // Copyright (c) jiezhang. all rights reserved. // import UIKitclass 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.html") 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 ()}}


At that time, I wanted to display the image, but the returned image was false. I checked the page and <big class = "jpg80 n01"> </big> shown in 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}
So the real address: Success.









Android Json parsing Server File

JSONObject aJosnObj;
AJosnObj = new JSONObject (stateJson );
JSONArray arrSpecialtys = aJosnObj. getJSONArray ("Stations ");
For (int I = 0; I <arrSpecialtys. length (); I ++ ){
JSONObject aJos = (JSONObject) arrSpecialtys. get (I );
String startingStation = aJos. getString ("StartingStation ");
String lat = aJos. getString ("Lat ");
}
String lineNum = aJos. getString ("LineNum ");

In this way, the "Emy of science and technology", 31.8308990633944 and 24 can be stored in startingStation, lat, and lineNum respectively.

How to parse json data with escape characters on the server using json?

The simplest JSON to String replacement character is converted back to JSON. Note the format to view the original post>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.