企评社

Interface labels are shown in English. Company reviews, forum posts and tool descriptions are currently available in Chinese only.

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 范围内Integernumberintint
整数但超出 int(±2147483648)Longnumberintint64
小数 / 科学计数法Doublenumberfloatfloat64
true / falseBooleanbooleanboolbool
字符串Stringstringstrstring
null(这一处是 null)ObjectanyAnyinterface{}
空数组 []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

Are the generated types accurate? What about nulls and empty arrays?

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.

Field names like user-name, class, 1st or Chinese - what is generated?

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.

How are JSON syntax errors reported? Do they locate the line?

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.

Which languages are supported? Is my JSON uploaded?

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.

Related tools