JSON 转多语言类生成器

将 JSON 示例转换为模型类或 TypeScript 接口。

// about

工具介绍

工具从 JSON 示例推断结构并生成代码。使用前请检查可选字段、嵌套对象、命名和类型。

如何使用

  1. 粘贴 JSON 对象或数组。
  2. 选择语言并输入根类名称。
  3. 点击生成,检查类型后复制代码。

人们为何使用此工具

  • 快速将 JSON API 响应转换为模型代码。
  • 支持 C#、Java、Python、JavaScript 和 TypeScript。
  • 可检查嵌套对象和数组结构。

常见问题解答 (FAQ)

生成的模型可以直接用于生产吗?

这是起始代码,请根据 API 契约检查字段、日期和空值。

指南 · 数据 · 约 3 分钟

修复 JSON 语法错误:7 个最常见的错误与 JSON 整理方法

API 响应、配置文件、日志——JSON 是开发者读得最多的数据格式。然而一个逗号或引号就可能引发 “Unexpected token” 错误。下面介绍如何快速找到原因。

JSON(JavaScript Object Notation)是由 RFC 8259 和 ECMA-404 标准化的文本格式。它源自 JavaScript,如今几乎所有语言都支持。值的类型只有六种:字符串、数字、布尔值(true/false)、null、数组和对象。

{
  "id": 1024,
  "name": "AIBIZAP",
  "active": true,
  "tags": ["tools", "guides"],
  "owner": null
}
JSON 基础知识、导致解析错误的七个错误、大数字和日期的陷阱,以及如何把 JSON 转换成类或数据库表。
阅读指南 →
用 AI 自己做

用 AI 亲手打造这个工具

想要自己的版本?把下面的任务提示词粘贴到 AI 聊天中,或将技能文件保存到 AI 编程助手。其中包含我们开发此工具时整理的功能、边界情况和测试用例。

提示词和技能以英文提供,AI 工具在任何语言环境下都能很好地理解。

AI 任务提示词

直接粘贴到 Claude、ChatGPT、Gemini 等 AI 聊天中即可。

You are an experienced full-stack web developer. Build a web tool called "JSON 类生成器" that meets the requirements below.

## Goal

Build a tool that generates C#, Java, Python and TypeScript model classes from a JSON sample.

## Core features

- Choose C#, Java, Python dataclasses or TypeScript interfaces
- Create separate classes for nested objects
- Infer array element types
- Set the root class name
- Apply each language's naming conventions

## Inputs

- JSON sample
- Target language and root class name

## Outputs

- Generated code
- Summary of inferred types

## Edge cases and handling rules

- Treat null values as nullable types
- Merge differing keys across array elements
- Handle keys that clash with reserved words
- Keep original names for keys with digits or hyphens via serialization attributes

## Implementation requirements

- Build it as a single page (HTML, CSS, JavaScript) that runs entirely in the browser, and never send the user's input to a server.
- Make the UI responsive and easy to use on mobile; label every input and keep everything keyboard accessible.
- Add a Copy button for results and show clear, friendly error messages for invalid input.
- Keep the core logic in functions separate from the UI so it can be unit-tested.

## Definition of done (test cases)

- {"id":1,"name":"a"} gives int Id and string Name in C#
- Nested objects become separate classes
- Empty arrays become lists of object/any

First outline your implementation plan in at most five lines, then write the complete code, and finally explain how to check the test cases above.

技能 (SKILL.md)

在 Claude Code 等 AI 编程助手中保存为 .claude/skills/build-json-class-generator/SKILL.md,以后开发或修改此工具时可重复使用。

---
name: build-json-class-generator
description: Use when building or changing a JSON 类生成器 tool. Build a tool that generates C#, Java, Python and TypeScript model classes from a JSON sample.
---

# Build: JSON 类生成器

## Goal

Build a tool that generates C#, Java, Python and TypeScript model classes from a JSON sample.

## Workflow

1. Review the required features, inputs and outputs below and ask about anything missing first.
2. Implement the core logic as pure functions separate from the UI.
3. Check the logic against the test cases in the verification list.
4. Build a responsive UI with an input form, a result area and a Copy button.
5. Reproduce each edge case listed under Rules and refine the error messages.

## Core features

- Choose C#, Java, Python dataclasses or TypeScript interfaces
- Create separate classes for nested objects
- Infer array element types
- Set the root class name
- Apply each language's naming conventions

## Inputs

- JSON sample
- Target language and root class name

## Outputs

- Generated code
- Summary of inferred types

## Rules

- Treat null values as nullable types
- Merge differing keys across array elements
- Handle keys that clash with reserved words
- Keep original names for keys with digits or hyphens via serialization attributes
- Build it as a single page (HTML, CSS, JavaScript) that runs entirely in the browser, and never send the user's input to a server.

## Verification

- [ ] {"id":1,"name":"a"} gives int Id and string Name in C#
- [ ] Nested objects become separate classes
- [ ] Empty arrays become lists of object/any

交给 AI 时的小技巧

  • 在第一行写明技术栈(如 React、Vue 或纯 HTML),生成的代码会更贴合你的项目。
  • 在接受代码前,请让 AI 运行列出的测试用例并展示结果。
  • 不要把真实密码、客户数据或 API 密钥粘贴到提示词中,请用示例值代替。