如何使用Alamofire请求和SwiftyJSON解析对JSON数组的JSON响应

时间:2019-05-09 12:13:43

标签: alamofire-request

我正在使用newsAPI获取所有新闻来源。我能够获得json响应,但无法将其转换为 json数组。请考虑我是ios开发的新手。

对于网络请求,我正在使用Alamofire并获得json响应。我为来源创建了一个模型。现在我想将所有响应转换为json数组,并将其存储到源类型的数组中。我在这里遇到问题。

json响应:

{"sources": [
  {
    "country" : "us",
    "language" : "en",
    "name" : "ABC News",
    "category" : "general",
    "id" : "abc-news",
    "url" : "https:\/\/abcnews.go.com",
    "description" : "Your trusted source for breaking news, analysis, exclusive interviews, headlines, and videos at ABCNews.com."
  },
  {
    "country" : "au",
    "language" : "en",
    "name" : "ABC News (AU)",
    "category" : "general",
    "id" : "abc-news-au",
    "url" : "http:\/\/www.abc.net.au\/news",
    "description" : "Australia's most trusted source of local, national and world news. Comprehensive, independent, in-depth analysis, the latest business, sport, weather and more."
  },

//我已经尝试过

guard let data = response.data else {return}

let jsonArray = try! JSON(data: data).array

print(jsonArray)

注意:然后我得到jsonArray的零值

0 个答案:

没有答案