JSON to entity class
Paste JSON and infer types automatically · generate Java / TypeScript / Python / Go / C# / PHP code
JSON 输入
对象或对象数组生成选项
生成结果
类型是怎么推断的
JSON 本身不带类型信息,工具按下面的规则读一遍你的数据(**规则就是这些,没有隐藏逻辑**):
| JSON 里的值 | Java | TypeScript | Python | Go |
|---|---|---|---|---|
| 整数且在 int 范围内 | Integer | number | int | int |
| 整数但超出 int(±2147483648) | Long | number | int | int64 |
| 小数 / 科学计数法 | Double | number | float | float64 |
| true / false | Boolean | boolean | bool | bool |
| 字符串 | String | string | str | string |
| null(这一处是 null) | Object | any | Any | interface{} |
| 空数组 [] | List<Object> | any[] | List[Any] | []interface{} |
| 数组 | List<元素类型> | 元素类型[] | List[元素类型] | []元素类型 |
| 嵌套对象 | 静态内部类 | 嵌套 interface | 独立 dataclass | 独立 struct |
类型冲突与缺失怎么处理(不静默选一个):
- 同一字段在不同数组元素里类型不同(例如一处是数字、一处是字符串):按"无法确定"处理,生成
Object/any,并在结果上方的提示里点名是哪个字段、出现了哪几种类型,由你决定改成什么。 - null:无法从 null 推断类型,单独出现时给
Object/any;与其它类型同时出现时按另一边的类型生成,并在注释里标注"可能为 null"。 - 空数组:不知道元素类型,给
List<Object>/any[]并提示。 - 某字段只在部分元素里出现:TypeScript 标为可选(
name?:),Java / C# / Go 在注释或 tag 里标注(Java 用Integer这类包装类型天然可空)。 - JSON 里的字段名不是合法标识符或撞上语言关键字(
user-name、class、1st、中文名):会改写成合法标识符,并尽最大可能保留原始 key——Java 用@JsonProperty、C# 用[JsonPropertyName]、Go 的json:"…"tag 一定带、Python 用field(metadata={"json": …})、TypeScript 干脆直接用原始 key 当属性名(带引号),因为 TS 没有注解机制、属性名就是运行时字段名。 - 改名后的映射代价(如实说明):Java / C# / Go 靠注解或 tag 自动映射,无需手工处理;TypeScript 用原始 key 所以天然正确;Python 的
metadata只有 dataclasses-json / pydantic 这类库会读,直接Root(**json.loads(x))遇到改名属性会报错;PHP 的json_decode按属性名严格映射,改名属性旁边会写/** 对应 JSON 字段: … */注释,序列化/反序列化时请据此手工处理(或改用数组访问)。 - 数字精度:超过 ±2^53 的整数在 JS 里已经没有精度,工具会提示你改用字符串(生成
String),不要用Long去接一个已经失真的值。 - 类名撞上语言内置类型名(例如字段叫
list派生出的类名List,会遮蔽 Java 的java.util.List/ C# 的System.Collections.Generic.List,直接编译不过):会自动加Model后缀(List→ListModel),同名字段仍然照常生成。
生成的代码是起点不是终点:Java 用 Lombok 还是手写 getter/setter、是否加 @JsonIgnoreProperties(ignoreUnknown = true)、日期字段用 String 还是 LocalDateTime,都请按你项目的规范微调。本工具不联网、不读取你项目的任何配置。
FAQ
JSON carries no type information, so the tool can only infer from the actual values in the data you pasted. A lone null cannot be inferred - it becomes Object / any; an empty array has no element type - it becomes List<Object> / any[]; when one field has different types across array elements, it is generated as Object / any marked indeterminate, and the notice above names the field and the types seen. The tool does not guess and does not silently pick for you - these become fields for you to confirm.
Each target language's rules are applied while preserving the original name as far as possible: Java rewrites to userName / class_ / _1stLogin plus @JsonProperty("user-name"); C# adds [JsonPropertyName]; Go always carries a json:"user-name" tag; Python uses a legal name like class_ plus field(metadata={"json": "user-name"}); TypeScript is simplest - the property is written as "user-name": string (TS has no annotation mechanism; the property name IS the runtime field name, and camelCasing it would break the match with JSON.parse). Chinese keys stay as-is (Java, Python and TypeScript all allow Chinese identifiers). Fair warning: Python's metadata is only read by libraries like dataclasses-json / pydantic, and PHP's json_decode maps strictly by property name - in those two languages, wire renamed fields by hand per the generated comments.
Yes. Validation uses our own strict parser, and errors give line number, column number and a plain-language note (for example: line 3, column 12: property names in objects must be double-quoted). Common traps get their own diagnostics: single-quoted strings, comments, trailing commas, numbers with leading zeros, misspelled true/false/null, unclosed brackets. The parser also caps nesting at 64 levels and 20,000 nodes - exceed them and it says so instead of freezing the page.
Six: Java (Lombok or plain getters/setters, optional Jackson @JsonProperty), TypeScript interface, Python dataclass, Go struct, C# class, PHP class. Nothing is uploaded: parsing, inference and generation all run in your browser - the data never leaves your machine, reloading clears it, the page contains no upload requests and no third-party dependencies (no CDN). If your JSON holds real names, phone numbers or ID numbers, local conversion is exactly about not sending those to any server.