Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用SwiftyJSON遍历数据_Ios_Json_Swift_Swifty Json - Fatal编程技术网

Ios 使用SwiftyJSON遍历数据

Ios 使用SwiftyJSON遍历数据,ios,json,swift,swifty-json,Ios,Json,Swift,Swifty Json,我从互联网上调用了一个JSON文件 { "Editions": [ { "Version": "November", "Articles": [ { "title": "hello", "subheading": "Article 1", "content": "stuff"

我从互联网上调用了一个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
}
希望我能解释清楚,希望你能帮忙

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

    }