【发布时间】:2015-11-05 16:39:43
【问题描述】:
我有一个从 Internet 调用的 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": ""
}
]
}
]
}
在我的第一个视图控制器上,我使用以下代码提取版本版本
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 将点击的版本传递给我的第二个视图控制器
在我的第二个视图控制器上完成此操作后,我可以打印出版本的字符串,在视图中确实加载了
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
}
希望我解释清楚,希望你能帮助
【问题讨论】:
标签: ios json swift swifty-json