2016-02-17 83 views
1

我試圖從Google新聞中檢索文章並使用Swift進行解析。我正在使用Alamofire。我知道什麼時候解析的基礎知識,但輸出看起來有點複雜,我不知道如何訪問「標題」值。任何幫助,將不勝感激。謝謝!需要幫助使用Alamofire解析JSON與Swift使用Alamofire

這裏是JSON輸出:


responseData =  { 
     cursor =   { 
      currentPageIndex = 0; 
      estimatedResultCount = 445755633; 
      moreResultsUrl = "http://news.google.com/nwshp?oe=utf8&ie=utf8&source=uds&q=sports&hl=en&start=0"; 
      pages =    (
           { 
        label = 1; 
        start = 0; 
       }, 
           { 
        label = 2; 
        start = 1; 
       }, 
           { 
        label = 3; 
        start = 2; 
       }, 
           { 
        label = 4; 
        start = 3; 
       }, 
           { 
        label = 5; 
        start = 4; 
       }, 
           { 
        label = 6; 
        start = 5; 
       }, 
           { 
        label = 7; 
        start = 6; 
       }, 
           { 
        label = 8; 
        start = 7; 
       } 
      ); 
     }; 
     results =   (
         { 
       GsearchResultClass = GnewsSearch; 
       clusterUrl = "http://news.google.com/news/story?ncl=dO-ivvqeXteuGKMAkRap55WyAkOvM&hl=en&ned=us"; 
       content = "sportsbook The Third Circuit Court of Appeals will rehear a potentially landmark case Wednesday brought by the NCAA, NFL, MLB, NBA and NHL claiming a New Jersey gambling law enacted in 2014 that repealed certain prohibitions on sports wagering in ..."; 
       image =     { 
        originalContextUrl = "http://lawnewz.com/sports/new-jersey-lawsuit-could-make-sports-betting-legal-across-the-country/"; 
        publisher = LawNewz; 
        tbHeight = 53; 
        tbUrl = "http://t0.gstatic.com/images?q=tbn:ANd9GcQ2htg8w11Y6_4ClDD80jdQN6VtGGMya5KomCyLCgzVjOsyMJLVdkffcSg9"; 
        tbWidth = 80; 
        url = "http://lawnewz.com/wp-content/uploads/2016/02/800px-Las_Vegas_sportsbook-351x234.jpg"; 
       }; 
       language = en; 
       location = ""; 
       publishedDate = "Tue, 16 Feb 2016 10:47:00 -0800"; 
       publisher = LawNewz; 
       relatedStories =     (
             { 
         language = en; 
         location = ""; 
         publishedDate = "Tue, 16 Feb 2016 07:34:35 -0800"; 
         publisher = ESPN; 
         signedRedirectUrl = "http://news.google.com/news/url?sa=T&ct2=us&fd=S&url=http://espn.go.com/chalk/story/_/id/14785562/chalk-previewing-latest-new-jersey-sports-betting-appeal&cid=52779048299261&ei=5OHDVujuMfOmwQHOn53oAg&usg=AFQjCNH7bfL6cWr1Q8PKgPOkQIP7ZRUoRw"; 
         title = "Future of sports betting once again at stake in New Jersey"; 
         titleNoFormatting = "Future of sports betting once again at stake in New Jersey"; 
         unescapedUrl = "http://espn.go.com/chalk/story/_/id/14785562/chalk-previewing-latest-new-jersey-sports-betting-appeal"; 
         url = "http%3A%2F%2Fespn.go.com%2Fchalk%2Fstory%2F_%2Fid%2F14785562%2Fchalk-previewing-latest-new-jersey-sports-betting-appeal"; 
        } 
       ); 
       signedRedirectUrl = "http://news.google.com/news/url?sa=T&ct2=us&fd=S&url=http://lawnewz.com/sports/new-jersey-lawsuit-could-make-sports-betting-legal-across-the-country/&cid=52779048299261&ei=5OHDVujuMfOmwQHOn53oAg&usg=AFQjCNHAqRF8uPjG1Ki7yxjz0sATv05i6A"; 
       title = "New Jersey Lawsuit Could Make Sports Betting Legal Across the Country"; 
       titleNoFormatting = "New Jersey Lawsuit Could Make Sports Betting Legal Across the Country"; 
       unescapedUrl = "http://lawnewz.com/sports/new-jersey-lawsuit-could-make-sports-betting-legal-across-the-country/"; 
       url = "http%3A%2F%2Flawnewz.com%2Fsports%2Fnew-jersey-lawsuit-could-make-sports-betting-legal-across-the-country%2F"; 
      } 
     ); 
    }; 
    responseDetails = ""; 
    responseStatus = 200; 
} 

我真的被這一點,因爲它看起來並不像我在網上看到的大多數例子混淆。

+0

解析'results'東西到自定義對象。儘可能少地將數據保持爲JSON。 – Sulthan

回答

0

從GitHub添加SwiftyJSON做以下

 import SwiftyJSON 

     func parseJson() { 

     Alamofire.request(.POST, "urlString", parameters: parameter, encoding: .JSON) 
     .responseJSON { response in 


      //Check Response 

      if response.result.isFailure == true { 
       return 
      } 

      let json = JSON(response.result.value!) 

      let cursorEntity = json["Cursor"] 
      let currentIndex = json["cursor"].currentPageIndex 
      let pageEntity = json["cursor"]["pages"] 
      let resultsEntity = json["cursor"]["results"] 
     } 
    } 
+0

謝謝!我玩了更多,最終得到了SwiftJSON並使用了你的方法。它現在很好用! – JChung37

+0

你的好友好友 –

0

使用Alamofire與SwiftyJSON插件叫做Alamofire-SwiftyJSON

Alamofire.request(.GET, 
    urlString, 
    parameters: ["foo": "bar"]) 
.responseSwiftyJSON({ (request, response, json, error) in 
    var cursor = json["cursor"] 
    // do your stuff .. 
}) 
0

由於@cem olcay以上使用Alamofire和SwiftyJSON提及。嘗試下面的代碼。但還沒有測試過。

func getData() { 
Alamofire.request(.GET, "YOUR_URL_STRING") 
     .responseJSON { response in 


let json = JSON(data: response) 
if let title = json[0]["relatedStories"][0]["title"].string { 
    //Now you got your value 
} 

     } 
}