使用 Swift 从 JSON 中仅提取一些字段
Extracting only some fields from a JSON with Swift
对不起,我是 Swift 编程新手。我在我的应用程序中使用 NewsAPI。他们的回复格式如下:
{
"status": "ok",
"totalResults": 38,
"articles": [
{
"source": {
"id": "cnn",
"name": "CNN"
},
"author": "Eric Levenson and Lauren del Valle, CNN",
"title": "Actress Annabella Sciorra is in court at Harvey Weinstein's trial for potential testimony - CNN",
"description": "Actress Annabella Sciorra is in court for Harvey Weinstein's criminal trial, setting up what could be the first face-to-face testimony from one of the women who has accused him of sexual assault.",
"url": "https://www.cnn.com/2020/01/23/us/harvey-weinstein-annabella-sciorra/index.html",
"urlToImage": "https://cdn.cnn.com/cnnnext/dam/assets/200123090608-annabella-sciorra-file-super-tease.jpg",
"publishedAt": "2020-01-23T16:22:00Z",
"content": "New York (CNN)Actress Annabella Sciorra is in court for Harvey Weinstein's criminal trial, setting up what could be the first face-to-face testimony from one of the women who has accused him of sexual assault.\r\n\"The Sopranos\" actress has said Weinstein raped … [+5398 chars]"
},
{
"source": {
"id": "ars-technica",
"name": "Ars Technica"
},
"author": "Jennifer Ouellette",
"title": "Jewel beetle’s bright colored shell serves as camouflage from predators - Ars Technica",
"description": "University of Bristol scientists offer first real evidence for a 100-year-old theory.",
"url": "https://arstechnica.com/science/2020/01/study-jewel-beetles-use-iridescence-for-camouflage-not-sexual-selection/",
"urlToImage": "https://cdn.arstechnica.net/wp-content/uploads/2020/01/beetleTOP-760x380.jpg",
"publishedAt": "2020-01-23T16:00:00Z",
"content": "Enlarge/ The brightly colored shell of this jewel beetle is a surprisingly effective form of camouflage, according to a new study by scientists at the University of Bristol.\r\n23 with 20 posters participating\r\nArtist and naturalist Abbott Handerson Thayer beca… [+5422 chars]"
}]
}
我有办法使用这些数据,但它涉及太多不必要的structs/classes。有没有办法让我仅将响应中的 articles
数组解码为这样定义的结构 Article
:
struct Article: Decodable {
let author: String?
let title: String?
let description: String?
let url: String?
let urlToImage: String?
let publishedAt: String?
}
感谢您的宝贵时间:)
是的,您可以只抓取您感兴趣的部分。
1。定义您的模型
定义模型时,您可以只声明您感兴趣的字段。
Since in a comment below you said you only want author
and title
for each Article
I updated the Article
struct accordingly.
struct Response: Decodable {
let articles: [Article]
struct Article: Decodable {
let author: String
let title: String
}
}
2。解码
do {
let response = try JSONDecoder().decode(Response.self, from: data)
let articles = response.articles
print(articles)
} catch {
print(error)
}
对不起,我是 Swift 编程新手。我在我的应用程序中使用 NewsAPI。他们的回复格式如下:
{
"status": "ok",
"totalResults": 38,
"articles": [
{
"source": {
"id": "cnn",
"name": "CNN"
},
"author": "Eric Levenson and Lauren del Valle, CNN",
"title": "Actress Annabella Sciorra is in court at Harvey Weinstein's trial for potential testimony - CNN",
"description": "Actress Annabella Sciorra is in court for Harvey Weinstein's criminal trial, setting up what could be the first face-to-face testimony from one of the women who has accused him of sexual assault.",
"url": "https://www.cnn.com/2020/01/23/us/harvey-weinstein-annabella-sciorra/index.html",
"urlToImage": "https://cdn.cnn.com/cnnnext/dam/assets/200123090608-annabella-sciorra-file-super-tease.jpg",
"publishedAt": "2020-01-23T16:22:00Z",
"content": "New York (CNN)Actress Annabella Sciorra is in court for Harvey Weinstein's criminal trial, setting up what could be the first face-to-face testimony from one of the women who has accused him of sexual assault.\r\n\"The Sopranos\" actress has said Weinstein raped … [+5398 chars]"
},
{
"source": {
"id": "ars-technica",
"name": "Ars Technica"
},
"author": "Jennifer Ouellette",
"title": "Jewel beetle’s bright colored shell serves as camouflage from predators - Ars Technica",
"description": "University of Bristol scientists offer first real evidence for a 100-year-old theory.",
"url": "https://arstechnica.com/science/2020/01/study-jewel-beetles-use-iridescence-for-camouflage-not-sexual-selection/",
"urlToImage": "https://cdn.arstechnica.net/wp-content/uploads/2020/01/beetleTOP-760x380.jpg",
"publishedAt": "2020-01-23T16:00:00Z",
"content": "Enlarge/ The brightly colored shell of this jewel beetle is a surprisingly effective form of camouflage, according to a new study by scientists at the University of Bristol.\r\n23 with 20 posters participating\r\nArtist and naturalist Abbott Handerson Thayer beca… [+5422 chars]"
}]
}
我有办法使用这些数据,但它涉及太多不必要的structs/classes。有没有办法让我仅将响应中的 articles
数组解码为这样定义的结构 Article
:
struct Article: Decodable {
let author: String?
let title: String?
let description: String?
let url: String?
let urlToImage: String?
let publishedAt: String?
}
感谢您的宝贵时间:)
是的,您可以只抓取您感兴趣的部分。
1。定义您的模型
定义模型时,您可以只声明您感兴趣的字段。
Since in a comment below you said you only want
author
andtitle
for eachArticle
I updated theArticle
struct accordingly.
struct Response: Decodable {
let articles: [Article]
struct Article: Decodable {
let author: String
let title: String
}
}
2。解码
do {
let response = try JSONDecoder().decode(Response.self, from: data)
let articles = response.articles
print(articles)
} catch {
print(error)
}