如何在Swift中没有键解码JSON数组
我有点新手Swift,所以如果我在这里错过了一些明显的东西,我深表歉意。我正在使用以这种格式返回JSON的API:
[
{
"title" : String
"image" : [
{
"is_leader": Bool
"urlPath": String
}
],
"body": String
},
{
"title" : String
"image" : [
{
"is_leader": Bool
"urlPath": String
}
]
"body": String
},
]
我似乎无法解码它。
这是我的模型:
struct ArticleData: Decodable {
let Articles: [Article]?
}
struct Article: Decodable {
let title: String?
let images: [Images]?
let body: String?
}
struct Images: Decodable {
let images: [Image]?
}
struct Image: Decodable {
let isLeader: Bool?
let url: String?
private enum CodingKeys: String, CodingKey {
case url, width, height
case isLeader = "is_Leader"
}
}
这是我运行时正在使用的解码器方法
do {
let decoder = JSONDecoder()
let jsonData = try decoder.decode([ArticleData.self], from: data)
,我只是得到零响应,而我只是不确定为什么。我肯定
会击中API的所有帮助!谢谢
I'm kinda new to Swift so I apologize if I'm missing something obvious here. I am using an API that returns a JSON in this format:
[
{
"title" : String
"image" : [
{
"is_leader": Bool
"urlPath": String
}
],
"body": String
},
{
"title" : String
"image" : [
{
"is_leader": Bool
"urlPath": String
}
]
"body": String
},
]
And I cant seem to decode it.
Here are my models:
struct ArticleData: Decodable {
let Articles: [Article]?
}
struct Article: Decodable {
let title: String?
let images: [Images]?
let body: String?
}
struct Images: Decodable {
let images: [Image]?
}
struct Image: Decodable {
let isLeader: Bool?
let url: String?
private enum CodingKeys: String, CodingKey {
case url, width, height
case isLeader = "is_Leader"
}
}
And this is the decoder method that I am using
do {
let decoder = JSONDecoder()
let jsonData = try decoder.decode([ArticleData.self], from: data)
When I run this I am just getting a nil response and I'm just im not really sure why. I am for sure hitting the API
All help is appreciated! Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修复程序是从
更改尝试dododer.decode([[articledata.self],从:data)
尝试decoder.decode([actits] .self,from:data)。
The fix is to change from
try decoder.decode([ArticleData.self], from: data)
totry decoder.decode([Article].self, from: data)
.Duplicate of Swift 4 json decode with top level array