JSON 입력
생성된 Swift
Swift 구조체를 생성하려면 JSON 데이터를 입력하세요
Codable 지원과 타입 안전성을 갖춘 구조체
JSON 데이터에서 Codable 지원 Swift 구조체 생성
Swift 구조체를 생성하려면 JSON 데이터를 입력하세요
Codable 지원과 타입 안전성을 갖춘 구조체
이 JSON-Swift 생성기를 사용하여 iOS/macOS 앱 및 Swift 서비스용 JSON 샘플에서 Codable Swift 구조체를 생성하세요.
1단계 - JSON 샘플 붙여넣기
Import을 사용하여 파일, URL 또는 샘플 데이터에서 JSON을 로드합니다.2단계 - Swift 구조체 옵션 선택
Struct Name(예: Root)을 설정합니다.String? 및 유사한 유형에 매핑되는 방식을 검토하세요.3단계 – 생성된 코드 검토
Root Type Name, null 처리 및 프레임워크와 같은 옵션을 조정합니다.4단계 - Codable과 함께 구조체 사용
Codable을 준수하는지 확인하세요.JSONDecoder을 사용하여 JSON을 루트 유형으로 디코딩합니다.CodingKeys 또는 날짜 디코딩 전략을 추가합니다.5단계 - 복사 또는 다운로드
빠른 팁
ISO8601DateFormatter/JSONDecoder.dateDecodingStrategy을(를) 선호합니다.CodingKeys을 사용하세요.// JSON 입력
{
"id": 123,
"name": "Maeve Winters",
"email": "[email protected]",
"active": true,
"roles": ["admin", "editor"],
"metadata": { "plan": "pro" },
"createdAt": "2024-03-01T10:15:00Z",
"score": 99.5,
"notes": null
}
// Generated Swift models (simplified)
struct Metadata: Codable {
let plan: String
}
struct Root: Codable {
let id: Int
let name: String
let email: String?
let active: Bool
let roles: [String]
let metadata: Metadata
let createdAt: String
let score: Double
let notes: String?
}이 JSON-Swift 생성기와 함께 훌륭하게 작동하는 더 많은 JSON 및 스키마 도구를 살펴보세요.
생성기는 올바른 타입 안전성을 가진 Swift struct를 생성하고, JSON 직렬화/역직렬화를 위한 Codable 프로토콜을 지원하며, null 값에는 optional 타입을 사용하고 Swift 네이밍 규칙을 따릅니다.
Codable 프레임워크를 선택하면 생성된 struct가 Codable을 자동으로 준수하여 JSONEncoder/JSONDecoder로 손쉽게 인코딩/디코딩할 수 있습니다.
JSON의 null 값은 Swift의 optional 타입(?)에 매핑되어 타입 안전성을 제공하며, Swift의 null 안전 접근 방식에 따라 명시적인 언래핑이 필요합니다.
생성된 Swift 코드는 Int, Double, String, Bool, [Any], [String: Any] 등 적절한 타입을 사용하고, 불변 데이터 모델을 위해 value semantics를 갖는 struct를 활용합니다.