在 JSON 中处理 RSS
Dealing with RSS in JSON
我有一个 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 的数组,然后显示在 table.
中
这是适用于所有 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()
}
我有一个 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 的数组,然后显示在 table.
中这是适用于所有 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()
}