- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 json:
"metatags": [
{
....
"og:url": "http://www.theguardian.com/world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful",
"article:author": "https://www.theguardian.com/profile/tomphillips",
"og:image:height": "720",
"og:description": "United States declines to criticise removal of term limits as liberal thinkers inside China say move deserves to be ‘mocked by civilised countries’",
"og:image:width": "1200",
"og:image": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxOC8wMS8zMS9mYWNlYm9va19kZWZhdWx0LnBuZw&s=391c7c897cafc35a55dd29b48ed544d5",
"al:ios:url": "gnmguardian://world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful?contenttype=Article&source=applinks",
"article:publisher": "https://www.facebook.com/theguardian",
"og:type": "article",
"al:ios:app_store_id": "409128287",
"article:section": "World news",
"article:published_time": "2018-02-27T03:36:30.000Z",
"og:title": "'Ensuring happier lives': Chinese media defends move to make Xi Jinping all powerful",
"fb:app_id": "180444840287",
"article:tag": "China,Xi Jinping,Asia Pacific,World news",
"al:ios:app_name": "The Guardian",
"og:site_name": "the Guardian",
....
}
]
这是对此API的回应,我想解析出响应中都存在的图像、标题、描述和站点名称
。这是到目前为止我创建的数据模型:
struct Metadata: Decodable{
var metatags: [enclosedTags]
}
struct enclosedTags: Decodable{
let image: String
let title: String
let description: String
let siteName: String
private enum codingKeys : String, CodingKey{
case image = "og:image", title = "og:title", description = "og:description", siteName = "og:site_name"
}
}
@IBAction func onSearch(_ sender: UIButton){
let session = URLSession.shared
let datatask = session.dataTask(with: request as URLRequest) { (data, response, error) in
guard let data = data else {
print ("Error: Handling URL request")
return }
do{
let tags = try JSONDecoder().decode(Metadata.self, from: data)
print(tags.metatags)
}
catch let jerror{
print("here", jerror)
}
}
datatask.resume()
}
使用此命令后,我收到以下错误:keyNotFound(Social_Login_api.Metadata.(_7FFB0C3B2CCB4BCEDB9D03985979711B中的CodingKeys).metatags,Swift.DecodingError.Context(codingPath:[],debugDescription:“没有与关键元标记关联的值(\“元标记\”)。“,underlyingError:nil))
我是否声明了结构错误,或者处理 JSON 请求是否存在问题,任何有用的见解都会很棒。
编辑1:这是请求的整个 JSON 响应,
{
"kind": "customsearch#search",
"url": {
"type": "application/json",
"template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
},
"queries": {
"request": [
{
"title": "Google Custom Search - 'Ensuring happier lives': Chinese media defends move to make Xi Jinping all powerful United States declines to criticise removal of term limits as liberal thinkers inside China say move deserves to be 'mocked by civilised countries' ",
"totalResults": "4",
"searchTerms": "'Ensuring happier lives': Chinese media defends move to make Xi Jinping all powerful United States declines to criticise removal of term limits as liberal thinkers inside China say move deserves to be 'mocked by civilised countries' ",
"count": 4,
"startIndex": 1,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "003086370729948886707:yvy2bcavdtu"
}
]
},
"context": {
"title": "*.google"
},
"searchInformation": {
"searchTime": 0.633407,
"formattedSearchTime": "0.63",
"totalResults": "4",
"formattedTotalResults": "4"
},
"items": [
{
"kind": "customsearch#result",
"title": "'Ensuring happier lives': Chinese media defends move to make Xi ...",
"htmlTitle": "'\u003cb\u003eEnsuring happier lives': Chinese media defends move\u003c/b\u003e to \u003cb\u003emake Xi\u003c/b\u003e ...",
"link": "https://www.theguardian.com/world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful",
"displayLink": "www.theguardian.com",
"snippet": "'Ensuring happier lives': Chinese media defends move to make Xi Jinping all \npowerful. United States declines to criticise removal of term limits as liberal \nthinkers inside China say move deserves to be 'mocked by civilised countries'. \nTom Phillips in Beijing. Mon 26 Feb 2018 22.36 EST Last modified on Mon 26 \nFeb 2018 ...",
"htmlSnippet": "'\u003cb\u003eEnsuring happier lives': Chinese media defends move\u003c/b\u003e to \u003cb\u003emake Xi Jinping all\u003c/b\u003e \u003cbr\u003e\n\u003cb\u003epowerful\u003c/b\u003e. \u003cb\u003eUnited States declines\u003c/b\u003e to \u003cb\u003ecriticise removal\u003c/b\u003e of \u003cb\u003eterm limits\u003c/b\u003e as \u003cb\u003eliberal\u003c/b\u003e \u003cbr\u003e\n\u003cb\u003ethinkers inside China say move deserves\u003c/b\u003e to be '\u003cb\u003emocked\u003c/b\u003e by \u003cb\u003ecivilised countries\u003c/b\u003e'. \u003cbr\u003e\nTom Phillips \u003cb\u003ein\u003c/b\u003e Beijing. Mon 26 Feb 2018 22.36 EST Last modified on Mon 26 \u003cbr\u003e\nFeb 2018 ...",
"cacheId": "x54qfhvXmSYJ",
"formattedUrl": "https://www.theguardian.com/.../ensuring-happier-lives-chinese-media- defends-move-to-make-xi-jinping-all-powerful",
"htmlFormattedUrl": "https://www.theguardian.com/.../\u003cb\u003eensuring\u003c/b\u003e-\u003cb\u003ehappier\u003c/b\u003e-\u003cb\u003elives\u003c/b\u003e-\u003cb\u003echinese\u003c/b\u003e-\u003cb\u003emedia\u003c/b\u003e- \u003cb\u003edefends\u003c/b\u003e-\u003cb\u003emove-to-make\u003c/b\u003e-\u003cb\u003exi\u003c/b\u003e-\u003cb\u003ejinping\u003c/b\u003e-\u003cb\u003eall\u003c/b\u003e-\u003cb\u003epowerful\u003c/b\u003e",
"pagemap": {
"thumbnail": [
{
"src": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=620&q=55&auto=format&usm=12&fit=max&s=c857db05131da665271beb49d600180d"
}
],
"metatags": [
{
"format-detection": "telephone=no",
"handheldfriendly": "True",
"viewport": "width=device-width,minimum-scale=1,initial-scale=1",
"apple-mobile-web-app-title": "Guardian",
"application-name": "The Guardian",
"msapplication-tilecolor": "#e7edef",
"theme-color": "#e7edef",
"msapplication-tileimage": "https://assets.guim.co.uk/images/favicons/77beb32f01ee0157ec193e09e4e18c4e/windows_tile_144_b.png",
"apple-itunes-app": "app-id=409128287, app-argument=https://www.theguardian.com/world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful, affiliate-data=ct=newsmartappbanner&pt=304191",
"author": "Tom Phillips",
"thumbnail": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=620&q=55&auto=format&usm=12&fit=max&s=c857db05131da665271beb49d600180d",
"news_keywords": "China,Xi Jinping,Asia Pacific,World news",
"og:url": "http://www.theguardian.com/world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful",
"article:author": "https://www.theguardian.com/profile/tomphillips",
"og:image:height": "720",
"og:description": "United States declines to criticise removal of term limits as liberal thinkers inside China say move deserves to be ‘mocked by civilised countries’",
"og:image:width": "1200",
"og:image": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxOC8wMS8zMS9mYWNlYm9va19kZWZhdWx0LnBuZw&s=391c7c897cafc35a55dd29b48ed544d5",
"al:ios:url": "gnmguardian://world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful?contenttype=Article&source=applinks",
"article:publisher": "https://www.facebook.com/theguardian",
"og:type": "article",
"al:ios:app_store_id": "409128287",
"article:section": "World news",
"article:published_time": "2018-02-27T03:36:30.000Z",
"og:title": "'Ensuring happier lives': Chinese media defends move to make Xi Jinping all powerful",
"fb:app_id": "180444840287",
"article:tag": "China,Xi Jinping,Asia Pacific,World news",
"al:ios:app_name": "The Guardian",
"og:site_name": "the Guardian",
"article:modified_time": "2018-02-27T03:37:57.000Z",
"twitter:app:id:iphone": "409128287",
"twitter:app:name:googleplay": "The Guardian",
"twitter:app:name:ipad": "The Guardian",
"twitter:image": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxOC8wMS8zMS90d2l0dGVyX2RlZmF1bHQucG5n&s=84952ae57ce9a937acbeb4ba307a1851",
"twitter:site": "@guardian",
"twitter:app:url:ipad": "gnmguardian://world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful?contenttype=Article&source=twitter",
"twitter:card": "summary_large_image",
"twitter:app:name:iphone": "The Guardian",
"twitter:app:id:ipad": "409128287",
"twitter:app:id:googleplay": "com.guardian",
"twitter:app:url:googleplay": "guardian://www.theguardian.com/world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful",
"twitter:app:url:iphone": "gnmguardian://world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful?contenttype=Article&source=twitter",
"twitter:dnt": "on",
"fb:pages": "10513336322"
}
],
"newsarticle": [
{
"mainentityofpage": "https://www.theguardian.com/world/2018/feb/27/ensuring-happier-lives-chinese-media-defends-move-to-make-xi-jinping-all-powerful",
"headline": "'Ensuring happier lives': Chinese media defends move to make Xi Jinping all powerful",
"description": "United States declines to criticise removal of term limits as liberal thinkers inside China say move deserves to be ‘mocked by civilised countries’",
"datepublished": "2018-02-26T22:36:30-0500",
"datemodified": "2018-02-26T22:37:57-0500",
"articlebody": "The Communist party’s decision to abolish presidential term limits – a move experts and critics have condemned as a lurch towards unchecked dictatorship in China – is designed to “ensure..."
}
],
"imageobject": [
{
"url": "https://uploads.guim.co.uk/2018/01/31/TheGuardian_AMP.png",
"width": "190",
"height": "60"
},
{
"representativeofpage": "true",
"url": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=700&q=55&auto=format&usm=12&fit=max&s=fe7f74933d50ef2a656efe950160ba56",
"width": "6835",
"height": "4101",
"contenturl": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=7b25be283cc4e2d1017f21fa387d7dd5",
"description": "People walk past a poster of Chinese President Xi Jinping beside a street in Beijing. The Communist party has paved the way for him to assume the presidency indefinitely. Photograph: Greg Baker/AFP..."
}
],
"person": [
{
"sameas": "Tom Phillips",
"name": "Tom Phillips"
}
],
"organization": [
{
"name": "The Guardian",
"sameas": "https://www.theguardian.com/"
}
],
"cse_image": [
{
"src": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=7b25be283cc4e2d1017f21fa387d7dd5",
"type": "1",
"width": "290",
"height": "174"
}
]
}
},
{
"kind": "customsearch#result",
"title": "China: Xi Jinping allowed to rule for ever - Europe Solidaire Sans ...",
"htmlTitle": "\u003cb\u003eChina\u003c/b\u003e: \u003cb\u003eXi Jinping\u003c/b\u003e allowed to rule for ever - Europe Solidaire Sans ...",
"link": "https://www.europe-solidaire.org/spip.php?article43289",
"displayLink": "www.europe-solidaire.org",
"snippet": "Feb 27, 2018 ... 'Ensuring happier lives': Chinese media defends move to make Xi Jinping all \npowerful. United States declines to criticise removal of term limits as liberal \nthinkers inside China say move deserves to be 'mocked by civilised countries'. \nThe Communist party's decision to abolish presidential term limits – a ...",
"htmlSnippet": "Feb 27, 2018 \u003cb\u003e...\u003c/b\u003e '\u003cb\u003eEnsuring happier lives\u003c/b\u003e': \u003cb\u003eChinese media defends move\u003c/b\u003e to \u003cb\u003emake Xi Jinping all\u003c/b\u003e \u003cbr\u003e\n\u003cb\u003epowerful\u003c/b\u003e. \u003cb\u003eUnited States declines\u003c/b\u003e to \u003cb\u003ecriticise removal\u003c/b\u003e of \u003cb\u003eterm limits\u003c/b\u003e as \u003cb\u003eliberal\u003c/b\u003e \u003cbr\u003e\n\u003cb\u003ethinkers inside China say move deserves\u003c/b\u003e to be '\u003cb\u003emocked\u003c/b\u003e by \u003cb\u003ecivilised countries\u003c/b\u003e'. \u003cbr\u003e\nThe Communist party's decision to abolish presidential \u003cb\u003eterm limits\u003c/b\u003e – a ...",
"cacheId": "xNfA2usjfGwJ",
"formattedUrl": "https://www.europe-solidaire.org/spip.php?article43289",
"htmlFormattedUrl": "https://www.europe-solidaire.org/spip.php?article43289",
"pagemap": {
"metatags": [
{
"viewport": "width=device-width, initial-scale=1.0"
}
],
"hcard": [
{
"fn": "PHILLIPS Tom",
"url": "https://www.europe-solidaire.org/spip.php?auteur10557",
"url_text": "PHILLIPS Tom"
}
]
}
},
{
"kind": "customsearch#result",
"title": "Xi Jinping | World | The Guardian",
"htmlTitle": "\u003cb\u003eXi Jinping\u003c/b\u003e | World | The Guardian",
"link": "https://www.theguardian.com/world/xi-jinping",
"displayLink": "www.theguardian.com",
"snippet": "1 day ago ... 'Ensuring happier lives': Chinese media defends move to make Xi Jinping all \npowerful. United States declines to criticise removal of term limits as liberal \nthinkers inside China say move deserves to be 'mocked by civilised countries'. \nPublished: 10:36 PM. 'Ensuring happier lives': Chinese media defends ...",
"htmlSnippet": "1 day ago \u003cb\u003e...\u003c/b\u003e '\u003cb\u003eEnsuring happier lives': Chinese media defends move\u003c/b\u003e to \u003cb\u003emake Xi Jinping all\u003c/b\u003e \u003cbr\u003e\n\u003cb\u003epowerful\u003c/b\u003e. \u003cb\u003eUnited States declines\u003c/b\u003e to \u003cb\u003ecriticise removal\u003c/b\u003e of \u003cb\u003eterm limits\u003c/b\u003e as \u003cb\u003eliberal\u003c/b\u003e \u003cbr\u003e\n\u003cb\u003ethinkers inside China say move deserves\u003c/b\u003e to be '\u003cb\u003emocked\u003c/b\u003e by \u003cb\u003ecivilised countries\u003c/b\u003e'. \u003cbr\u003e\nPublished: 10:36 PM. '\u003cb\u003eEnsuring happier lives': Chinese media defends\u003c/b\u003e ...",
"cacheId": "UjbuXbyQHKsJ",
"formattedUrl": "https://www.theguardian.com/world/xi-jinping",
"htmlFormattedUrl": "https://www.theguardian.com/world/\u003cb\u003exi\u003c/b\u003e-\u003cb\u003ejinping\u003c/b\u003e",
"pagemap": {
"cse_thumbnail": [
{
"width": "290",
"height": "174",
"src": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcToDkj5l17V62vsyZ0lkbCiH6Er9PEydDqKzxA7yPuOB5irCw3rceke9ks"
}
],
"metatags": [
{
"format-detection": "telephone=no",
"handheldfriendly": "True",
"viewport": "width=device-width,minimum-scale=1,initial-scale=1",
"apple-mobile-web-app-title": "Guardian",
"application-name": "The Guardian",
"msapplication-tilecolor": "#e7edef",
"theme-color": "#e7edef",
"msapplication-tileimage": "https://assets.guim.co.uk/images/favicons/77beb32f01ee0157ec193e09e4e18c4e/windows_tile_144_b.png",
"apple-itunes-app": "app-id=409128287, app-argument=https://www.theguardian.com/world/xi-jinping, affiliate-data=ct=newsmartappbanner&pt=304191",
"og:url": "http://www.theguardian.com/world/xi-jinping",
"al:ios:url": "gnmguardian://world/xi-jinping?contenttype=list&source=applinks",
"og:type": "website",
"al:ios:app_store_id": "409128287",
"fb:app_id": "180444840287",
"al:ios:app_name": "The Guardian",
"og:site_name": "the Guardian",
"twitter:app:id:iphone": "409128287",
"twitter:app:name:googleplay": "The Guardian",
"twitter:app:name:ipad": "The Guardian",
"twitter:site": "@guardian",
"twitter:app:url:ipad": "gnmguardian://world/xi-jinping?contenttype=list&source=twitter",
"twitter:card": "summary",
"twitter:app:name:iphone": "The Guardian",
"twitter:app:id:ipad": "409128287",
"twitter:app:id:googleplay": "com.guardian",
"twitter:app:url:iphone": "gnmguardian://world/xi-jinping?contenttype=list&source=twitter",
"twitter:dnt": "on",
"fb:pages": "10513336322",
"twitter:url": "https://www.theguardian.com/world/xi-jinping",
"og:title": "Xi Jinping | World news | The Guardian"
}
],
"webpage": [
{
"keywords": "Asia Pacific"
}
],
"cse_image": [
{
"src": "https://i.guim.co.uk/img/media/07a39d19a47e2e87502cd64d2ea09cd841d59a4a/591_660_4794_2876/master/4794.jpg?w=300&q=55&auto=format&usm=12&fit=max&s=ea982bfe9f0599c02e30198d63d0a1a8"
}
]
}
},
{
"kind": "customsearch#result",
"title": "مارس 2015 – علوم انسانی olomeensani.com",
"htmlTitle": "مارس 2015 – علوم انسانی olomeensani.com",
"link": "http://olomeensani.com/2015/03",
"displayLink": "olomeensani.com",
"snippet": "30 مارس 2015 ... Kate Lamb in Jakarta; 'Ensuring happier lives': Chinese media defends move to \nmake Xi Jinping all powerful فوریه 27, 2018. United States declines to criticise \nremoval of term limits as liberal thinkers inside China say move deserves to be '\nmocked by civilised countries'The Communist party's decision to ...",
"htmlSnippet": "30 مارس 2015 \u003cb\u003e...\u003c/b\u003e Kate Lamb \u003cb\u003ein\u003c/b\u003e Jakarta; '\u003cb\u003eEnsuring happier lives\u003c/b\u003e': \u003cb\u003eChinese media defends move\u003c/b\u003e to \u003cbr\u003e\n\u003cb\u003emake Xi Jinping all powerful\u003c/b\u003e فوریه 27, 2018. \u003cb\u003eUnited States declines\u003c/b\u003e to \u003cb\u003ecriticise\u003c/b\u003e \u003cbr\u003e\n\u003cb\u003eremoval\u003c/b\u003e of \u003cb\u003eterm limits\u003c/b\u003e as \u003cb\u003eliberal thinkers inside China say move deserves\u003c/b\u003e to be '\u003cbr\u003e\n\u003cb\u003emocked\u003c/b\u003e by \u003cb\u003ecivilised countries\u003c/b\u003e'The Communist party's decision to ...",
"cacheId": "j9CqlbDCdQAJ",
"formattedUrl": "olomeensani.com/2015/03",
"htmlFormattedUrl": "olomeensani.com/2015/03",
"pagemap": {
"metatags": [
{
"viewport": "width=device-width, initial-scale=1",
"msapplication-tileimage": "http://olomeensani.com/wp-content/uploads/2015/12/cropped-DSC_0187.JPG-e1449088658125-1-270x270.jpg"
}
]
}
}
]
}
最终编辑:
您先生让我很高兴,很高兴看到这个结果,
Optional([Social_Login_api.Item(metatags: Optional([Social_Login_api.enclosedTags(image: Optional("https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxOC8wMS8zMS9mYWNlYm9va19kZWZhdWx0LnBuZw&s=391c7c897cafc35a55dd29b48ed544d5"), title: Optional("\'Ensuring happier lives\': Chinese media defends move to make Xi Jinping all powerful"), description: Optional("United States declines to criticise removal of term limits as liberal thinkers inside China say move deserves to be ‘mocked by civilised countries’"), siteName: Optional("the Guardian"))]))
最后您能否指出如何在 json 提取字符串之前摆脱这些烦人的可选措辞?
最佳答案
在代码中,
struct Metadata: Decodable
{
var metatags: [enclosedTags]?
}
struct enclosedTags: Decodable
{
let image: String?
let title: String?
let description: String?
let siteName: String?
private enum CodingKeys : String, CodingKey
{
case image = "og:image", title = "og:title", description = "og:description", siteName = "og:site_name"
}
}
它必须是 CodingKeys
而不是 codingKeys
。它是“C”而不是“c”
此外,可编码类型的属性,即Metadata
和enheldTags
必须是可选
。您不知道将从 API 中得到什么。如果属性不是可选的,并且您没有从 API 获取任何一个键,则完整的对象将为 nil
。
编辑:
使用尝试?
而不是尝试
let tags = try? JSONDecoder().decode(Metadata.self, from: data)
print(tags?.metatags)
预期的 JSON
格式:
{
"metatags": [
{
"og:description": "United States declines to criticise removal of term limits as liberal thinkers inside China say move deserves to be ‘mocked by civilised countries’",
"og:image": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxOC8wMS8zMS9mYWNlYm9va19kZWZhdWx0LnBuZw&s=391c7c897cafc35a55dd29b48ed544d5",
"og:title": "'Ensuring happier lives': Chinese media defends move to make Xi Jinping all powerful",
}
]
}
示例代码:
let jsonString = """
{
"metatags": [
{
"og:description": "United States declines to criticise removal of term limits as liberal thinkers inside China say move deserves to be ‘mocked by civilised countries’",
"og:image": "https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxOC8wMS8zMS9mYWNlYm9va19kZWZhdWx0LnBuZw&s=391c7c897cafc35a55dd29b48ed544d5",
"og:title": "'Ensuring happier lives': Chinese media defends move to make Xi Jinping all powerful",
}
]
}
"""
if let jsonData = jsonString.data(using: .utf8)
{
let obj = try? JSONDecoder().decode(Metadata.self, from: jsonData)
print(obj?.metatags)
}
输出:
[SampleNavigation.ViewController.enclosedTags(image: Optional("https://i.guim.co.uk/img/media/5d33d9276cf4388564b5bf4d42e0cd4ad8ec221e/0_230_6835_4101/master/6835.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxOC8wMS8zMS9mYWNlYm9va19kZWZhdWx0LnBuZw&s=391c7c897cafc35a55dd29b48ed544d5"), title: Optional("\'Ensuring happier lives\': Chinese media defends move to make Xi Jinping all powerful"), description: Optional("United States declines to criticise removal of term limits as liberal thinkers inside China say move deserves to be ‘mocked by civilised countries’"), siteName: nil)]
编辑2:
struct Response: Decodable
{
var items: [Item]?
}
struct Item: Decodable
{
var pagemap: Pagemap?
}
struct Pagemap: Decodable
{
var metatags: [enclosedTags]?
}
struct enclosedTags: Decodable
{
let image: String?
let title: String?
let description: String?
let siteName: String?
private enum CodingKeys : String, CodingKey
{
case image = "og:image", title = "og:title", description = "og:description", siteName = "og:site_name"
}
}
解码 JSON:
if let jsonData = jsonString.data(using: .utf8)
{
let obj = try? JSONDecoder().decode(Response.self, from: jsonData)
print(obj?.items)
}
编辑3:
您可以使用容器
来提取嵌套信息,而不是为pagemap
创建单独的结构,即您可以使用以下 结构
,
struct Response: Decodable
{
var items: [Item]?
}
struct Item: Decodable
{
var metatags: [enclosedTags]?
enum CodingKeys : String, CodingKey
{
case pagemap
}
enum PageMapKeys: String, CodingKey
{
case metatags
}
init(from decoder: Decoder) throws
{
let values = try decoder.container(keyedBy: CodingKeys.self)
let pagemap = try values.nestedContainer(keyedBy: PageMapKeys.self, forKey: .pagemap)
metatags = try pagemap.decode([enclosedTags].self, forKey: .metatags)
}
}
struct enclosedTags: Decodable
{
let image: String?
let title: String?
let description: String?
let siteName: String?
private enum CodingKeys : String, CodingKey
{
case image = "og:image", title = "og:title", description = "og:description", siteName = "og:site_name"
}
}
如果您仍然遇到任何问题,请告诉我。快乐编码..🙂
关于json - 使用可解码协议(protocol)在 Swift 4 中解码 Google 自定义搜索 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49358645/
internal protocol Reducer { associatedtype S : BaseState associatedtype A : BaseActi
我在考虑我的应用程序中的验证检查,我认为在任何模型上调用 ValidatorFactory,实现 Validee,这意味着说哪个类负责 ValidatorCreation 听起来不错。但是下面的代码不
我已经定义了 2 个协议(protocol)。我需要第一个 (NameProtocol) 来执行 Equatable 协议(protocol)。而另一个类 (BuilderProtocol) 有一个返
在上传方面,WebDAV 协议(protocol)在哪些方面优于 HTTP 协议(protocol)。 Socket Upload 协议(protocol)和 WebDav Upload 协议(pro
是否可以在任何版本的 Swift 中扩展具有混合类/协议(protocol)类型约束的协议(protocol)?例如,仅当 Self 是 UIViewController 的子类并且符合 Protoc
我有一个协议(protocol) (ProtocolA),其中包含符合第二个协议(protocol) (ProtocolB) 的单个属性。 public protocol ProtocolA {
NSObject 协议(protocol)带有常用的协议(protocol)模板,但它似乎并不是协议(protocol)实际实现所必需的。将其排除在外似乎完全没有任何改变。那么,协议(protocol
我想根据这两种协议(protocol)的一般特征(例如开销(数据包)、安全性、信息建模和可靠性)来比较 OPC UA 和 MQTT。我在哪里可以找到每个协议(protocol)的开销和其他特性的一些示
使用 Swift 4,我正在尝试编写一个自定义协议(protocol),它提供对 @objc 协议(protocol)的一致性。 一些代码 更具体地说,我有一个自定义协议(protocol) Sear
我想定义一个在 Viper 架构中使用的协议(protocol),以使用具有弱属性的协议(protocol)在 Viper 组件之间建立连接,但我收到以下错误消息: 'weak' may only b
我在同一个网络中有 3 个 docker 容器: 存储 (golang) - 它提供了用于上传视频文件的 API。 主播 (nginx) - 它流式传输上传的文件 反向代理 (姑且称之为代理) 我有
我打算在我的项目中使用 php socket。它需要用户登录才能根据 session 填充内容。所以我的问题是,TCP/IP 协议(protocol)也像 HTTP 协议(protocol)一样为每个
目前,我的网站有两个版本。一种带有 https://-证书,一种没有。我想将我网站的 http 版本上的所有用户 301 重定向到我网站的 https://版本。 这似乎不可能,因为创建重定向将导致重
目前,我的网站有两个版本。一种带有 https://-证书,一种没有。我想将我网站的 http 版本上的所有用户 301 重定向到我网站的 https://版本。 这似乎不可能,因为创建重定向将导致重
我有一个 Swift View Controller ,它定义了一个在 Objective-C View Controller 中应该遵循的协议(protocol): ChildViewControl
我在客户那里有数百个硬件设备,需要通过telnet接口(interface)发送HTTP数据。 目标是等待数据的 Apache 2 Web 服务器和 PHP 脚本。 这已经可以正常工作了,但是我们发现
我发现如果我创建一个这样的协议(protocol): protocol MyProtocol { } 我不能这样做: weak var myVar: MyProtocol? 我找到了解决这个问题的方法
Xcode 基于模板生成了这个头文件: // this file is XYZAppDelegate.h #import @interface XYZAppDelegate : UIRespond
我在 github 中有一个公开的存储库,我正在开发一个开源应用程序,用于制作产品目录和小型 cms 内容。 我还有一个私有(private)仓库(不托管在github),它是在托管在github的开
您好,我想让别人看到私有(private) repo 代码,但不想公开我的 repo ,也不希望他们有能力更改内容。这可能吗?我查看了网站的“管理”部分,但没有找到合适的内容。谢谢大家。 最佳答案 据
我是一名优秀的程序员,十分优秀!