需要帮助使用Alamofire使用Swift解析JSON

编程入门 行业动态 更新时间:2024-10-04 21:16:58
本文介绍了需要帮助使用Alamofire使用Swift解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从Google新闻中检索一篇文章,并使用Swift对其进行解析.我正在使用Alamofire.我知道解析时的基础知识,但是输出似乎有点复杂,而且我不知道如何访问"title"值.任何帮助,将不胜感激.谢谢!

I'm trying to retrieve an article from Google news and parse it using Swift. I'm using Alamofire. I know the basics of what to when parsing, but the output seems kind of complex and I don't know how to access the "title" value. Any help would be appreciated. Thanks!

这是JSON输出:

responseData = { cursor = { currentPageIndex = 0; estimatedResultCount = 445755633; moreResultsUrl = "news.google/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 = "news.google/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 = "lawnewz/sports/new-jersey-lawsuit-could-make-sports-betting-legal-across-the-country/"; publisher = LawNewz; tbHeight = 53; tbUrl = "t0.gstatic/images?q=tbn:ANd9GcQ2htg8w11Y6_4ClDD80jdQN6VtGGMya5KomCyLCgzVjOsyMJLVdkffcSg9"; tbWidth = 80; url = "lawnewz/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 = "news.google/news/url?sa=T&ct2=us&fd=S&url=espn.go/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 = "espn.go/chalk/story/_/id/14785562/chalk-previewing-latest-new-jersey-sports-betting-appeal"; url = "http%3A%2F%2Fespn.go%2Fchalk%2Fstory%2F_%2Fid%2F14785562%2Fchalk-previewing-latest-new-jersey-sports-betting-appeal"; } ); signedRedirectUrl = "news.google/news/url?sa=T&ct2=us&fd=S&url=lawnewz/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 = "lawnewz/sports/new-jersey-lawsuit-could-make-sports-betting-legal-across-the-country/"; url = "http%3A%2F%2Flawnewz%2Fsports%2Fnew-jersey-lawsuit-could-make-sports-betting-legal-across-the-country%2F"; } ); }; responseDetails = ""; responseStatus = 200; }

我对此感到非常困惑,因为它看起来不像我在网上看到的大多数示例.

I'm really confused by this because it doesn't look like most examples I've seen online.

推荐答案

从github添加SwiftyJSON并执行以下操作

add SwiftyJSON from github and do the stuff below

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

更多推荐

需要帮助使用Alamofire使用Swift解析JSON

本文发布于:2023-11-26 09:42:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Alamofire   Swift   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!