我在async中调用了一个JSON数据我的问题是,如何从标签内的所有人调用标题
{
"rss": {
"channel": {
"atom:link": "",
"title": "The Local",
"link": "http://www.thelocal.se/",
"description": "Sweden's news in English",
"language": "en-us",
"managingEditor": "TheLocal",
"webMaster": "TheLocal",
"generator": "TheLocal RSS Feed Generator",
"item": {
"title": "Knausgård savages the 'Cyclops' Swedes",
"description": "Norwegian literary star Karl Ove Knausgård has launched an extraordinary attack on the Swedes, damning them as a race of narrow-minded “cyclops” who cannot tolerate ambiguity, have no understanding of literature, and are “full of hate and fear”.",
"link": "http://www.thelocal.se/20150520/knausgrd-savages-the-cyclops-swedes",
"pubDate": "2015-05-20 15:25:25",
"guid": "http://www.thelocal.se/20150520/knausgrd-savages-the-cyclops-swedes"
},
"item": {
"title": "Migration Board worker took bribes for passports",
"description": "A court in Malmö has jailed a former Migration Board worker and his accomplice for taking bribes in exchange for residence permits and passports for asylum seekers desperate to stay in Sweden.",
"link": "http://www.thelocal.se/20150520/two-found-guilty-in-migration-board-bribery-case",
"pubDate": "2015-05-20 12:32:46",
"guid": "http://www.thelocal.se/20150520/two-found-guilty-in-migration-board-bribery-case"
},
"item": {
"title": "Two million Swedes design 'house of clicks'",
"description": "A team of award-winning architects have joined forces with two million Swedes to design the country’s most sought-after home.",
"link": "http://www.thelocal.se/20150520/two-million-swedes-design-dream-house-of-clicks",
"pubDate": "2015-05-20 12:01:35",
"guid": "http://www.thelocal.se/20150520/two-million-swedes-design-dream-house-of-clicks"
},
我现在拥有的是:
if var jsonArray = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {
println("jsonArray: \(jsonArray)")
dispatch_async(dispatch_get_main_queue()) {
for var i:Int = 0; i < 10; i++ {
var title:String = jsonArray.valueForKeyPath("rss.channel.item.title") as String
self.tableData.append(title)
var content:String = jsonArray.valueForKeyPath("rss.channel.item.description") as String
self.detail.append(content)
self.rssTable.reloadData()
}
}
问题是,无论我怎么想,我都不能调用多个项目,因为json没有排序,并且使用某种形式的数组可能就是答案。
任何想法如何解决数据。数据进入一个名为tableData的数组,然后显示在表格中。
答案 0 :(得分:0)
这是适用于所有RSS源的解决方案。
for var i:Int = 0; i < 10; i++ {
var title:String = jsonArray.valueForKeyPath("rss.channel.item:\(i).title") as String
self.tableData.append(title)
var content:String = jsonArray.valueForKeyPath("rss.channel.item:\(i).description") as String
self.detail.append(content)
var link:String = jsonArray.valueForKeyPath("rss.channel.item:\(i).link") as String
self.linkData.append(link)
self.rssTable.reloadData()
}