我之前发过这个问题 Swift: Parsing Json string and populating information into a dictionary
但我现在有一个稍微不同的问题。所以我再次发布这个修改过的问题。我是一名新开发人员并且正在尝试学习SWIFT。
我在swift程序中调用Web服务http://www.kuakes.com/json/,如下所示
override func viewDidLoad() {
super.viewDidLoad()
let httpMethod = "GET"
/* We have a 15-second timeout for our connection */
let timeout = 15
var urlAsString = "http://www.kuakes.com/json/"
let url = NSURL(string: urlAsString)
/* Set the timeout on our request here */
let urlRequest = NSMutableURLRequest(URL: url,
cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 15.0)
urlRequest.HTTPMethod = httpMethod
let queue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(urlRequest,
queue: queue,
completionHandler: {(response: NSURLResponse!,
data: NSData!,
error: NSError!) in
if data.length > 0 && error == nil{
let html = NSString(data: data, encoding: NSUTF8StringEncoding)
println("html = \(html)")
} else if data.length == 0 && error == nil{
println("Nothing was downloaded")
} else if error != nil{
println("Error happened = \(error)")
}
}
并获得以下结果
[{"响应":1,"消息":" OK""计数" 50},{&#34 ; id":145608," title":" M 0.9 爆炸 - 莫顿19公里E, 华盛顿""连结":" HTTP://earthquake.usgs.gov/earthquakes/eventpage/uw60985917","源":" HTTP://www.kuakes.com","北":46.536999,"西":122.021004," LAT":46.536499," LNG": - 122.021164,"深度":0," MAG":0.9,"时间":" 2015年4月10日 21:26:07 UTC"," timestamp":1428701167},{" id":145609," title":" M 2.3 - 27公里W的锚点, 阿拉斯加""连结":" HTTP://earthquake.usgs.gov/earthquakes/eventpage/ak11550832","源":" HTTP://www.kuakes.com","北":59.820999,"西":152.307999," LAT":59.820702," LNG": - 152.308197,"深度":59," MAG":2.3,"时间":" 2015年4月10日 20:30:09 UTC"," timestamp":1428697809},{" id":145610," title":" M 2.2 - 距离Red Bluff 23公里ENE, 加利福尼亚""连结":" HTTP://earthquake.usgs.gov/earthquakes/eventpage/nc72429666","源":" HTTP://www.kuakes.com","北":40.293999,"西":122.000999," LAT":40.294167," LNG": - 122.001335,"深度":10," MAG":2.2,"时间":" 2015年4月10日 20:19:01 UTC"," timestamp":1428697141},{" id":145611," title":" M 2.8 - 54km N of Dorado,Puerto 波多黎各""连结":" HTTP://earthquake.usgs.gov/earthquakes/eventpage/pr15100004","源":" HTTP://www.kuakes.com","北":18.951,"西":66.219002," LAT":18.951401," LNG": - 66.219101,"深度":28," MAG":2.8,"时间":" 2015年4月10日 20:05:46 UTC"," timestamp":1428696346},
我想将上面的json结果解析为ONE字典对象,例如key将是id,值将是相应字符串的其余部分。所以字典对象的第一个条目如下:
id = 145609"
标题:" M 2.3 - 27公里W的锚点, 阿拉斯加""连结":" HTTP://earthquake.usgs.gov/earthquakes/eventpage/ak11550832","源":" HTTP://www.kuakes.com","北":59.820999,"西":152.307999," LAT":59.820702," LNG": - 152.308197,"深度":59," MAG":2.3,"时间":" 2015年4月10日 20:30:09 UTC"," timestamp":1428697809}
字典中的第二个条目就是这个
id = 145609
"标题":" M 2.2 - 红崖的23公里ENE, 加利福尼亚""连结":" HTTP://earthquake.usgs.gov/earthquakes/eventpage/nc72429666","源":" HTTP://www.kuakes.com","北":40.293999,"西":122.000999," LAT":40.294167," LNG": - 122.001335,"深度":10," MAG":2.2,"时间":" 2015年4月10日 20:19:01 UTC"," timestamp":1428697141}
依旧等等......
我想暂时解析并填充单个字典对象中的键/值对。
答案 0 :(得分:1)
我建议你使用像SwiftyJSON这样的东西。
然后创建一个这样的类来存储东西:
class Entry {
var title, link, source, north, west, lat, lng, dept, mag, time, timestamp
}