- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试将 Json 解析为模型,但显示有关类型不匹配的错误消息。
typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))
附加模型,您可以在下面找到 Json。
JSON - http://api.duckduckgo.com/?q=the+wire+characters&format=json
import Foundation
struct RootClass : Codable {
let type : String?
let imageWidth : Int?
let relatedTopics : [RelatedTopics]?
let answer : String?
let definitionSource : String?
let abstractURL : String?
let meta : Meta?
let answerType : String?
let abstractSource : String?
let imageIsLogo : Int?
let image : String?
let heading : String?
let definitionURL : String?
let definition : String?
let infobox : String?
let redirect : String?
let results : [String]?
let abstract : String?
let imageHeight : Int?
let entity : String?
let abstractText : String?
enum CodingKeys: String, CodingKey {
case type = "Type"
case imageWidth = "ImageWidth"
case relatedTopics = "RelatedTopics"
case answer = "Answer"
case definitionSource = "DefinitionSource"
case abstractURL = "AbstractURL"
case meta = "meta"
case answerType = "AnswerType"
case abstractSource = "AbstractSource"
case imageIsLogo = "ImageIsLogo"
case image = "Image"
case heading = "Heading"
case definitionURL = "DefinitionURL"
case definition = "Definition"
case infobox = "Infobox"
case redirect = "Redirect"
case results = "Results"
case abstract = "Abstract"
case imageHeight = "ImageHeight"
case entity = "Entity"
case abstractText = "AbstractText"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
type = try values.decodeIfPresent(String.self, forKey: .type)
imageWidth = try values.decodeIfPresent(Int.self, forKey: .imageWidth)
relatedTopics = try values.decodeIfPresent([RelatedTopics].self, forKey: .relatedTopics)
answer = try values.decodeIfPresent(String.self, forKey: .answer)
definitionSource = try values.decodeIfPresent(String.self, forKey: .definitionSource)
abstractURL = try values.decodeIfPresent(String.self, forKey: .abstractURL)
meta = try values.decodeIfPresent(Meta.self, forKey: .meta)
answerType = try values.decodeIfPresent(String.self, forKey: .answerType)
abstractSource = try values.decodeIfPresent(String.self, forKey: .abstractSource)
imageIsLogo = try values.decodeIfPresent(Int.self, forKey: .imageIsLogo)
image = try values.decodeIfPresent(String.self, forKey: .image)
heading = try values.decodeIfPresent(String.self, forKey: .heading)
definitionURL = try values.decodeIfPresent(String.self, forKey: .definitionURL)
definition = try values.decodeIfPresent(String.self, forKey: .definition)
infobox = try values.decodeIfPresent(String.self, forKey: .infobox)
redirect = try values.decodeIfPresent(String.self, forKey: .redirect)
results = try values.decodeIfPresent([String].self, forKey: .results)
abstract = try values.decodeIfPresent(String.self, forKey: .abstract)
imageHeight = try values.decodeIfPresent(Int.self, forKey: .imageHeight)
entity = try values.decodeIfPresent(String.self, forKey: .entity)
abstractText = try values.decodeIfPresent(String.self, forKey: .abstractText)
}
}
struct RelatedTopics : Codable {
let icon : Icon?
let text : String?
let result : String?
let firstURL : String?
enum CodingKeys: String, CodingKey {
case icon = "Icon"
case text = "Text"
case result = "Result"
case firstURL = "FirstURL"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
icon = try values.decodeIfPresent(Icon.self, forKey: .icon)
text = try values.decodeIfPresent(String.self, forKey: .text)
result = try values.decodeIfPresent(String.self, forKey: .result)
firstURL = try values.decodeIfPresent(String.self, forKey: .firstURL)
}
}
struct Developer : Codable {
let url : String?
let name : String?
let type : String?
enum CodingKeys: String, CodingKey {
case url = "url"
case name = "name"
case type = "type"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
url = try values.decodeIfPresent(String.self, forKey: .url)
name = try values.decodeIfPresent(String.self, forKey: .name)
type = try values.decodeIfPresent(String.self, forKey: .type)
}
}
struct Maintainer : Codable {
let github : String?
enum CodingKeys: String, CodingKey {
case github = "github"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
github = try values.decodeIfPresent(String.self, forKey: .github)
}
}
struct SrcOptions : Codable {
let skip_abstract_paren : Int?
let language : String?
let is_mediawiki : Int?
let src_info : String?
let skip_icon : Int?
let source_skip : String?
let min_abstract_length : String?
let directory : String?
let skip_end : String?
let is_fanon : Int?
let skip_qr : String?
let skip_abstract : Int?
let is_wikipedia : Int?
let skip_image_name : Int?
enum CodingKeys: String, CodingKey {
case skip_abstract_paren = "skip_abstract_paren"
case language = "language"
case is_mediawiki = "is_mediawiki"
case src_info = "src_info"
case skip_icon = "skip_icon"
case source_skip = "source_skip"
case min_abstract_length = "min_abstract_length"
case directory = "directory"
case skip_end = "skip_end"
case is_fanon = "is_fanon"
case skip_qr = "skip_qr"
case skip_abstract = "skip_abstract"
case is_wikipedia = "is_wikipedia"
case skip_image_name = "skip_image_name"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
skip_abstract_paren = try values.decodeIfPresent(Int.self, forKey: .skip_abstract_paren)
language = try values.decodeIfPresent(String.self, forKey: .language)
is_mediawiki = try values.decodeIfPresent(Int.self, forKey: .is_mediawiki)
src_info = try values.decodeIfPresent(String.self, forKey: .src_info)
skip_icon = try values.decodeIfPresent(Int.self, forKey: .skip_icon)
source_skip = try values.decodeIfPresent(String.self, forKey: .source_skip)
min_abstract_length = try values.decodeIfPresent(String.self, forKey: .min_abstract_length)
directory = try values.decodeIfPresent(String.self, forKey: .directory)
skip_end = try values.decodeIfPresent(String.self, forKey: .skip_end)
is_fanon = try values.decodeIfPresent(Int.self, forKey: .is_fanon)
skip_qr = try values.decodeIfPresent(String.self, forKey: .skip_qr)
skip_abstract = try values.decodeIfPresent(Int.self, forKey: .skip_abstract)
is_wikipedia = try values.decodeIfPresent(Int.self, forKey: .is_wikipedia)
skip_image_name = try values.decodeIfPresent(Int.self, forKey: .skip_image_name)
}
}
struct Meta : Codable {
let created_date : String?
let topic : [String]?
let src_name : String?
let status : String?
let src_options : SrcOptions?
let producer : String?
let dev_milestone : String?
let perl_module : String?
let dev_date : String?
let attribution : String?
let maintainer : Maintainer?
let blockgroup : String?
let example_query : String?
let js_callback_name : String?
let is_stackexchange : String?
let description : String?
let name : String?
let designer : String?
let signal_from : String?
let repo : String?
let live_date : String?
let developer : [Developer]?
let src_id : Int?
let src_domain : String?
let tab : String?
let unsafe : Int?
let src_url : String?
let id : String?
let production_state : String?
enum CodingKeys: String, CodingKey {
case created_date = "created_date"
case topic = "topic"
case src_name = "src_name"
case status = "status"
case src_options = "src_options"
case producer = "producer"
case dev_milestone = "dev_milestone"
case perl_module = "perl_module"
case dev_date = "dev_date"
case attribution = "attribution"
case maintainer = "maintainer"
case blockgroup = "blockgroup"
case example_query = "example_query"
case js_callback_name = "js_callback_name"
case is_stackexchange = "is_stackexchange"
case description = "description"
case name = "name"
case designer = "designer"
case signal_from = "signal_from"
case repo = "repo"
case live_date = "live_date"
case developer = "developer"
case src_id = "src_id"
case src_domain = "src_domain"
case tab = "tab"
case unsafe = "unsafe"
case src_url = "src_url"
case id = "id"
case production_state = "production_state"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
created_date = try values.decodeIfPresent(String.self, forKey: .created_date)
topic = try values.decodeIfPresent([String].self, forKey: .topic)
src_name = try values.decodeIfPresent(String.self, forKey: .src_name)
status = try values.decodeIfPresent(String.self, forKey: .status)
src_options = try values.decodeIfPresent(SrcOptions.self, forKey: .src_options)
producer = try values.decodeIfPresent(String.self, forKey: .producer)
dev_milestone = try values.decodeIfPresent(String.self, forKey: .dev_milestone)
perl_module = try values.decodeIfPresent(String.self, forKey: .perl_module)
dev_date = try values.decodeIfPresent(String.self, forKey: .dev_date)
attribution = try values.decodeIfPresent(String.self, forKey: .attribution)
maintainer = try values.decodeIfPresent(Maintainer.self, forKey: .maintainer)
blockgroup = try values.decodeIfPresent(String.self, forKey: .blockgroup)
example_query = try values.decodeIfPresent(String.self, forKey: .example_query)
js_callback_name = try values.decodeIfPresent(String.self, forKey: .js_callback_name)
is_stackexchange = try values.decodeIfPresent(String.self, forKey: .is_stackexchange)
description = try values.decodeIfPresent(String.self, forKey: .description)
name = try values.decodeIfPresent(String.self, forKey: .name)
designer = try values.decodeIfPresent(String.self, forKey: .designer)
signal_from = try values.decodeIfPresent(String.self, forKey: .signal_from)
repo = try values.decodeIfPresent(String.self, forKey: .repo)
live_date = try values.decodeIfPresent(String.self, forKey: .live_date)
developer = try values.decodeIfPresent([Developer].self, forKey: .developer)
src_id = try values.decodeIfPresent(Int.self, forKey: .src_id)
src_domain = try values.decodeIfPresent(String.self, forKey: .src_domain)
tab = try values.decodeIfPresent(String.self, forKey: .tab)
unsafe = try values.decodeIfPresent(Int.self, forKey: .unsafe)
src_url = try values.decodeIfPresent(String.self, forKey: .src_url)
id = try values.decodeIfPresent(String.self, forKey: .id)
production_state = try values.decodeIfPresent(String.self, forKey: .production_state)
}
}
struct Icon : Codable {
let height : String?
let uRL : String?
let width : String?
enum CodingKeys: String, CodingKey {
case height = "Height"
case uRL = "URL"
case width = "Width"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
height = try values.decodeIfPresent(String.self, forKey: .height)
uRL = try values.decodeIfPresent(String.self, forKey: .uRL)
width = try values.decodeIfPresent(String.self, forKey: .width)
}
}
class Service: NSObject{
static let shared = Service()
func fetchCourses(completion: @escaping ([RootClass]?, Error?) -> ()) {
let urlString = "http://api.duckduckgo.com/?q=the+wire+characters&format=json"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, resp, err) in
if let err = err {
completion(nil, err)
print("Failed to fetch courses:", err)
return
}
// check response
guard let data = data else { return }
do {
let courses = try JSONDecoder().decode([RootClass].self, from: data)
DispatchQueue.main.async {
completion(courses, nil)
}
} catch let jsonErr {
print("Failed to decode:", jsonErr)
}
}.resume()
}
}
错误:
Failed to decode: typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))
最佳答案
替换(您的根 json 对象以 {}
开头,它是字典而不是数组 []
)
let courses = try JSONDecoder().decode([RootClass].self, from: data)
与
let courses = try JSONDecoder().decode(RootClass.self, from: data)
<小时/>
而且你不必编写 init,因为所有都是可选的
import Foundation
struct RootClass : Codable {
let type : String?
let imageWidth : Int?
let relatedTopics : [RelatedTopics]?
let answer : String?
let definitionSource : String?
let abstractURL : String?
let meta : Meta?
let answerType : String?
let abstractSource : String?
let imageIsLogo : Int?
let image : String?
let heading : String?
let definitionURL : String?
let definition : String?
let infobox : String?
let redirect : String?
let results : [String]?
let abstract : String?
let imageHeight : Int?
let entity : String?
let abstractText : String?
enum CodingKeys: String, CodingKey {
case type = "Type"
case imageWidth = "ImageWidth"
case relatedTopics = "RelatedTopics"
case answer = "Answer"
case definitionSource = "DefinitionSource"
case abstractURL = "AbstractURL"
case meta = "meta"
case answerType = "AnswerType"
case abstractSource = "AbstractSource"
case imageIsLogo = "ImageIsLogo"
case image = "Image"
case heading = "Heading"
case definitionURL = "DefinitionURL"
case definition = "Definition"
case infobox = "Infobox"
case redirect = "Redirect"
case results = "Results"
case abstract = "Abstract"
case imageHeight = "ImageHeight"
case entity = "Entity"
case abstractText = "AbstractText"
}
}
struct RelatedTopics : Codable {
let icon : Icon?
let text : String?
let result : String?
let firstURL : String?
enum CodingKeys: String, CodingKey {
case icon = "Icon"
case text = "Text"
case result = "Result"
case firstURL = "FirstURL"
}
}
struct Developer : Codable {
let url : String?
let name : String?
let type : String?
}
struct Maintainer : Codable {
let github : String?
}
struct SrcOptions : Codable {
let skip_abstract_paren : Int?
let language : String?
let is_mediawiki : Int?
let src_info : String?
let skip_icon : Int?
let source_skip : String?
let min_abstract_length : String?
let directory : String?
let skip_end : String?
let is_fanon : Int?
let skip_qr : String?
let skip_abstract : Int?
let is_wikipedia : Int?
let skip_image_name : Int?
enum CodingKeys: String, CodingKey {
case skip_abstract_paren = "skip_abstract_paren"
case language = "language"
case is_mediawiki = "is_mediawiki"
case src_info = "src_info"
case skip_icon = "skip_icon"
case source_skip = "source_skip"
case min_abstract_length = "min_abstract_length"
case directory = "directory"
case skip_end = "skip_end"
case is_fanon = "is_fanon"
case skip_qr = "skip_qr"
case skip_abstract = "skip_abstract"
case is_wikipedia = "is_wikipedia"
case skip_image_name = "skip_image_name"
}
}
struct Meta : Codable {
let created_date : String?
let topic : [String]?
let src_name : String?
let status : String?
let src_options : SrcOptions?
let producer : String?
let dev_milestone : String?
let perl_module : String?
let dev_date : String?
let attribution : String?
let maintainer : Maintainer?
let blockgroup : String?
let example_query : String?
let js_callback_name : String?
let is_stackexchange : String?
let description : String?
let name : String?
let designer : String?
let signal_from : String?
let repo : String?
let live_date : String?
let developer : [Developer]?
let src_id : Int?
let src_domain : String?
let tab : String?
let unsafe : Int?
let src_url : String?
let id : String?
let production_state : String?
enum CodingKeys: String, CodingKey {
case created_date = "created_date"
case topic = "topic"
case src_name = "src_name"
case status = "status"
case src_options = "src_options"
case producer = "producer"
case dev_milestone = "dev_milestone"
case perl_module = "perl_module"
case dev_date = "dev_date"
case attribution = "attribution"
case maintainer = "maintainer"
case blockgroup = "blockgroup"
case example_query = "example_query"
case js_callback_name = "js_callback_name"
case is_stackexchange = "is_stackexchange"
case description = "description"
case name = "name"
case designer = "designer"
case signal_from = "signal_from"
case repo = "repo"
case live_date = "live_date"
case developer = "developer"
case src_id = "src_id"
case src_domain = "src_domain"
case tab = "tab"
case unsafe = "unsafe"
case src_url = "src_url"
case id = "id"
case production_state = "production_state"
}
}
struct Icon : Codable {
let height : String?
let uRL : String?
let width : String?
enum CodingKeys: String, CodingKey {
case height = "Height"
case uRL = "URL"
case width = "Width"
}
}
关于json - 字典 JSON 的 SWIFT JSONDecoder 类型不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56738505/
最近开始学习MongoDB。今天老师教了我们 mongoexport 命令。在练习时,我遇到了一个典型的问题,包括教练在内的其他同学都没有遇到过。我在我的 Windows 10 机器上使用 Mongo
我是 JSON Schema 的新手,读过什么是 JSON Schema 等等。但我不知道如何将 JSON Schema 链接到 JSON 以针对该 JSON Schema 进行验证。谁能解释一下?
在 xml 中,我可以在另一个 xml 文件中包含一个文件并使用它。如果您的软件从 xml 获取配置文件但没有任何方法来分离配置,如 apache/ngnix(nginx.conf - site-av
我有一个 JSON 对象,其中包含一个本身是 JSON 对象的字符串。我如何反序列化它? 我希望能够做类似的事情: #[derive(Deserialize)] struct B { c: S
考虑以下 JSON { "a": "{\"b\": 12, \"c\": \"test\"}" } 我想定义一个泛型读取 Reads[Outer[T]]对于这种序列化的 Json import
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 11 个月前关闭。 Improve
我的旧项目在 MySQL 中有 Standard JSON 格式的数据。 对于我在 JS (Node.js) 和 DynamoDB 中的全新项目,关于 Standard JSON格式: 是否建议将其转
JSON 值字符串、数字、true、false、null 是否是有效的 JSON? 即,是 true 一个有效的 JSON 文档?还是必须是数组/对象? 一些验证器接受这个(例如 http://jso
我有一个 JSON 字符串,其中一个字段是文本字段。这个文本字段可以包含用户在 UI 中输入的文本,如果他们输入的文本是 JSON 文本,也许是为了说明一些编码,我需要对他们的文本进行编码,以便它不会
我正在通过 IBM MQ 调用处理数据,当由 ColdFusion 10 (10,0,11,285437) 序列化时,0 将作为 +0.0 返回,它会导致无效的 JSON并且无法反序列化。 stPol
我正在从三个数组中生成一个散列,然后尝试构建一个 json。我通过 json object has array 成功了。 require 'json' A = [['A1', 'A2', 'A3'],
我从 API 接收 JSON,响应可以是 30 种类型之一。每种类型都有一组唯一的字段,但所有响应都有一个字段 type 说明它是哪种类型。 我的方法是使用serde .我为每种响应类型创建一个结构并
我正在下载一个 JSON 文件,我已将其检查为带有“https://jsonlint.com”的有效 JSON 到文档目录。然后我打开文件并再次检查,结果显示为无效的 JSON。这怎么可能????这是
我正在尝试根据从 API 接收到的数据动态创建一个 JSON 对象。 收到的示例数据:将数据解码到下面给出的 CiItems 结构中 { "class_name": "test", "
我想从字符串转换为对象。 来自 {"key1": "{\n \"key2\": \"value2\",\n \"key3\": {\n \"key4\": \"value4\"\n }\n
目前我正在使用以下代码将嵌套的 json 转换为扁平化的 json: import ( "fmt" "github.com/nytlabs/gojsonexplode" ) func
我有一个使用来自第三方 API 的数据的应用程序。我需要将 json 解码为一个结构,这需要该结构具有“传入”json 字段的 json 标签。传出的 json 字段具有不同的命名约定,因此我需要不同
我想使用 JSON 架构来验证某些值。我有两个对象,称它们为 trackedItems 和 trackedItemGroups。 trackedItemGroups 是组名称和 trackedItem
考虑以下案例类模式, case class Y (a: String, b: String) case class X (dummy: String, b: Y) 字段b是可选的,我的一些数据集没有字
我正在存储 cat ~/path/to/file/blah | 的输出jq tojson 在一个变量中,稍后在带有 JSON 内容的 curl POST 中使用。它运作良好,但它删除了所有换行符。我知
我是一名优秀的程序员,十分优秀!