2015-11-05 4 views
2

У меня есть JSON файл, который я называю из Интернетатраверс данных с использованием SwiftyJSON

{ 
    "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]

Однако может я цикл через все Editions и использовать строку только для печати статей в соответствии с этой версией

Например

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

Надежда Я объяснил себя хорошо, надеясь, что вы можете помочь

ответ

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

    } 
Смежные вопросы