如何在Swift中没有键解码JSON数组

发布于 2025-02-11 00:21:50 字数 1230 浏览 3 评论 0原文

我有点新手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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

oО清风挽发oО 2025-02-18 00:21:50

修复程序是从更改尝试dododer.decode([[articledata.self],从:data) 尝试decoder.decode([actits] .self,from:data)。

The fix is to change from try decoder.decode([ArticleData.self], from: data) to try decoder.decode([Article].self, from: data).

Duplicate of Swift 4 json decode with top level array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文