🕊️ JSON 转 Swift CodableJSON to Swift Codable
根据 JSON 生成 Swift 的 Codable 结构体(struct 或 class),自动生成 CodingKeys 映射 snake_case 键名,可空字段用可选类型,混合类型生成 JSONValue 枚举。Generate Swift Codable structs or classes from JSON, with CodingKeys for snake_case keys, optionals for missing or null fields and a JSONValue enum for mixed types.
使用说明与常见问题How-to & FAQ
「JSON 转 Swift Codable」是什么?What is the JSON to Swift Codable tool?
根据 JSON 生成 Swift 的 Codable 结构体(struct 或 class),自动生成 CodingKeys 映射 snake_case 键名,可空字段用可选类型,混合类型生成 JSONValue 枚举。Generate Swift Codable structs or classes from JSON, with CodingKeys for snake_case keys, optionals for missing or null fields and a JSONValue enum for mixed types.
如何使用「JSON 转 Swift Codable」?How do I use the JSON to Swift Codable?
粘贴 JSON,选择 struct 或 class、let 或 var,下方即生成可直接用 JSONDecoder 解码的 Swift 代码。例如 "user_name" 会变成 userName,并在 CodingKeys 里写 case userName = "user_name"。Paste JSON, choose struct or class and let or var, and you get Swift code ready for JSONDecoder. For example "user_name" becomes userName with case userName = "user_name" in CodingKeys.
为什么要生成 CodingKeys?Why are CodingKeys generated?
Swift 属性习惯用驼峰命名,而 JSON 常用 snake_case。只要有键名被改过名,就会生成 CodingKeys 枚举,把属性和原始键名一一对应;也可以改用 JSONDecoder 的 keyDecodingStrategy = .convertFromSnakeCase。Swift properties are camelCase while JSON often uses snake_case. Whenever a key is renamed, a CodingKeys enum maps each property to the original key; alternatively use keyDecodingStrategy = .convertFromSnakeCase.
数组里各元素字段不一样怎么办?What if array elements have different fields?
工具会合并数组里所有对象的字段,只在部分元素中出现的字段生成可选类型(?),整数和小数混用时统一为 Double。All objects in the array are merged; fields present in only some elements become optionals, and mixed integers and decimals become Double.
「JSON 转 Swift Codable」免费吗?数据会上传吗?Is the JSON to Swift Codable free? Is my data uploaded?
「JSON 转 Swift Codable」完全免费,无需注册或安装,手机和电脑都能用;所有处理都在你的浏览器本地完成,输入的内容和文件不会上传到服务器。The JSON to Swift Codable is completely free with no sign-up or install and works on phones and desktops; everything runs locally in your browser and nothing you enter is uploaded.