-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decode.swift
More file actions
42 lines (37 loc) · 1.14 KB
/
test_decode.swift
File metadata and controls
42 lines (37 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import Foundation
struct NewsArticle: Codable, Identifiable {
var id: String { url.isEmpty ? UUID().uuidString : url }
let title_en: String?
let title_ja: String?
let description_en: String?
let description_ja: String?
let url: String
let urlToImage: String?
let publishedAt: String?
let source: String?
let lang: String?
}
let jsonString = """
[
{
"description_en": "When I hear \\"AI gadget\\", I can't help...",
"description_ja": "AIガジェット...",
"lang": "en",
"publishedAt": "2026-03-01T04:00:00Z",
"source": "ギズモード・ジャパン",
"title_en": "Why I'm Not Getting My Hopes Up",
"title_ja": "Appleが開発中...",
"url": "https://www.gizmodo.jp/2026/",
"urlToImage": "https://media.loom-app.com/"
}
]
"""
do {
let data = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let articles = try decoder.decode([NewsArticle].self, from: data)
print("Success! Decoded \(articles.count) articles.")
print("Article 1 URL: \(articles[0].url)")
} catch {
print("Error decoding: \\(error)")
}