作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个从互联网调用的 JSON 文件
{
"Editions": [
{
"Version": "November",
"Articles": [
{
"title": "hello",
"subheading": "Article 1",
"content": "stuff",
"author": "John Smith",
"authorDescription": "Author",
"image": "pic1.jpg"
},
{
"title": "article2",
"subheading": "Article 2",
"content": "stuff2",
"author": "first name last name",
"authorDescription": "Author",
"image": ""
},
{
"title": "article3",
"subheading": "Article 3",
"content": "stuff3",
"author": "Callum West",
"authorDescription": "Author",
"image": ""
},
{
"title": "Article 4",
"subheading": "Article 4",
"content": "stuff 4",
"author": "tom C",
"authorDescription": "Author",
"image": ""
}
]
},
{
"Version": "October",
"Articles": [
{
"title": "article 1",
"subheading": "Article1",
"content": "stuff1.1",
"author": "Tom",
"authorDescription": "Author",
"image": ""
},
{
"title": "article2",
"subheading": "Article 2",
"content": "stuff2.1",
"author": "Sam",
"authorDescription": "Author",
"image": ""
},
{
"title": "article3",
"subheading": "Article 3",
"content": "stuff3.1",
"author": "TomC",
"authorDescription": "Author and Editor",
"image": ""
},
{
"title": "article 4",
"subheading": "Article 4",
"content": "stuff4.1",
"author": "brad name",
"authorDescription": "Author",
"image": ""
},
{
"title": "article5",
"subheading": "Article 5",
"content": "stuff5.1",
"author": "chris evuan",
"authorDescription": "Author",
"image": ""
},
{
"title": "article6",
"subheading": "Article 6",
"content": "stuff6.1",
"author": "Jo",
"authorDescription": "Author",
"image": ""
},
{
"title": "article7",
"subheading": "Article7",
"content": "stuff7.1",
"author": "Tom Hall",
"authorDescription": "Author",
"image": ""
}
]
}
]
}
在我的第一个 View Controller 上,我使用以下代码提取版本
func loaddata(){
Alamofire.request(.GET, "my JSON url")
.responseJSON { response in
//get json from response data
//print (response.data)
let json = JSON(data: response.data!)
//print(json)
//for loop over json and write all article titles articles array
for (key, subJson) in json["Editions"] {
let version = subJson["Version"].string
let stuff = Edition(Version: version!)
// print(stuff)
self.editions.append(stuff!)
}
// let x = self.editions[0].Version
// print ("\(x)")
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
然后我使用 segue 将点击的版本传递到我的第二个 View Controller
在我的第二个 View Controller 上完成此操作后,我可以打印出版本的字符串,在 View 中加载
let worked = pleasework as String!
//print("\(worked)")
然后我想用这个字符串遍历Json,拉出相关的内容
所以我调用函数并传递给它
loaddata("\(worked)")
这里是加载数据函数
func loaddata(verionTitle: String){
Alamofire.request(.GET, "my JSON url")
.responseJSON { response in
//get json from response data
let json = JSON(data: response.data!)
// print(json)
//for loop over json and write all article titles articles array
// print("\(verionTitle)")
for (key, subJson) in json["Editions"][0]["Articles"]{
// print(subJson)
//let versionmuted = version as String!
//print("\(version)")
//if verionTitle =
//if version == verionTitle{
//print("The month is \(version)")
let author = subJson["title"].string
//print("\(author)")
let subheading = subJson["subheading"].string
let content = subJson["content"].string
let Author = subJson["author"].string
//let image = subJson["image"].string
let stuff = Article(name: author!, subheading: subheading!, content: content!, author: Author!)
self.articles.append(stuff!)
}
//end iff
//if let content = subJson["content"].string {
// self.Content.append(content)
//}
//end for
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
我正在努力循环遍历 JSON 并且只提取与版本相关的文章,我正在使用 swiftyjson 库
当前函数显然打印出Editions[0]中的所有文章
但是我可以遍历所有版本并使用字符串只打印该版本下的文章
例如
for (key, subJson) in json["Editions"][0]["Version"] = veriontitle??{
//do stuff
}
希望我解释得很好,希望你能帮忙
最佳答案
func loaddata(verionTitle: String){
Alamofire.request(.GET, "your json")
.responseJSON { response in
//get json from response data
let json = JSON(data: response.data!)
//print(json)
//start looping function pass through identifer from month, i've set October to 1 and so forth
func looping(Number: Int){
for (key, subJson) in json["Editions"][Number]["Articles"]{
let author = subJson["title"].string
//print("\(author)")
let subheading = subJson["subheading"].string
let content = subJson["content"].string
let Author = subJson["author"].string
let Image = subJson["image"].string
let stuff = Article(name: author!, subheading: subheading!, content: content!, author: Author!, image: Image!)
self.articles.append(stuff!)
//end loop
}
//end looping
}
//Get Version Titles, here is where I assign months to a number releavnt to where they are in the json
switch verionTitle{
case "November":
looping(0)
case "October":
looping(1)
case "December":
looping(2)
default:
print("no month")
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
关于ios - 使用 SwiftyJSON 遍历数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33550075/
我是一名优秀的程序员,十分优秀!