💠 JSON 转 Ruby 类JSON to Ruby Class
根据 JSON 生成 Ruby 类:attr_accessor 属性、from_hash / from_json 解析(嵌套对象和对象数组自动转换)、to_h / to_json 序列化,也可以生成更简洁的 Struct 写法。Generate Ruby classes from JSON: attr_accessor attributes, from_hash / from_json parsing (nested objects and arrays converted automatically) and to_h / to_json serialization, or a concise Struct version.
使用说明与常见问题How-to & FAQ
「JSON 转 Ruby 类」是什么?What is the JSON to Ruby Class tool?
根据 JSON 生成 Ruby 类:attr_accessor 属性、from_hash / from_json 解析(嵌套对象和对象数组自动转换)、to_h / to_json 序列化,也可以生成更简洁的 Struct 写法。Generate Ruby classes from JSON: attr_accessor attributes, from_hash / from_json parsing (nested objects and arrays converted automatically) and to_h / to_json serialization, or a concise Struct version.
如何使用「JSON 转 Ruby 类」?How do I use the JSON to Ruby Class?
粘贴 JSON 并填写根类名,选择 class 或 Struct 风格,下方生成 Ruby 代码,用 Root.from_json(text) 解析,root.to_json 输出。Paste JSON, set the root class name and choose class or Struct style; parse with Root.from_json(text) and serialize with root.to_json.
为什么有的字段名后面加了下划线?Why do some names end in an underscore?
class、end、hash 等是 Ruby 关键字或 Object 已有的方法,直接作为属性名会出错或覆盖内置行为,工具会改成 class_ 这样的名字,序列化时仍使用原始键名。Names like class, end or hash are Ruby keywords or core Object methods, so the tool renames them (e.g. class_) while still using the original key in JSON.
class 和 Struct 风格有什么区别?class vs Struct style?
class 风格是最常见的普通类,便于以后添加方法和校验;Struct 风格更简洁,自带 ==、to_a 等方法,适合纯数据对象。The class style is a plain class that is easy to extend with methods and validation; the Struct style is more concise and comes with ==, to_a and friends — good for pure data objects.
「JSON 转 Ruby 类」免费吗?数据会上传吗?Is the JSON to Ruby Class free? Is my data uploaded?
「JSON 转 Ruby 类」完全免费,无需注册或安装,手机和电脑都能用;所有处理都在你的浏览器本地完成,输入的内容和文件不会上传到服务器。The JSON to Ruby Class 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.