使用SwiftyJSON遍历数据

时间:2015-11-05 16:39:43

标签: ios json swift swifty-json

我有一个我从互联网上调用的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库

当前功能显然打印出版本[0]

中的所有文章

但是,我可以遍历所有版本并使用该字符串仅打印该版本下的文章

例如

for (key, subJson) in json["Editions"][0]["Version"] = veriontitle??{
 //do stuff
}

希望我能很好地解释自己,希望你能帮忙

1 个答案:

答案 0 :(得分:0)

 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()
                }
        }

    }