🔷

JSON 转 C# 工具

转换器

从样例 JSON 自动推断生成 C# 类(支持 Newtonsoft.Json 与 System.Text.Json 特性),处理嵌套对象与数组类型。适合对接 RESTful API 写模型层、C# 后端开发者省去手敲类定义。

json-to-go.input
json-to-go.output
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Profile
{
        [JsonProperty("age")]
        public int Age { get; set; }
        [JsonProperty("city")]
        public string? City { get; set; }
}

public class RootObject
{
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("name")]
        public string? Name { get; set; }
        [JsonProperty("active")]
        public bool Active { get; set; }
        [JsonProperty("score")]
        public double Score { get; set; }
        [JsonProperty("tags")]
        public List<string>? Tags { get; set; }
        [JsonProperty("profile")]
        public Profile Profile { get; set; }
}

关于 JSON 转 C# 工具

JSON 转 C# 代码工具在浏览器本地解析样例 JSON 并自动生成完整的 C# 类定义。字段名转为 PascalCase 命名风格,嵌套对象拆分为独立 class,数组推断为 List 泛型,支持 nullable 引用类型(C# 8.0+)和可空值类型。可选添加 JsonProperty 特性以兼容 System.Text.Json 和 Newtonsoft.Json 两种序列化框架,保留原始 JSON 键名作为特性参数。生成的代码可直接用于 .NET Web API 模型、EF Core 实体和 REST 客户端 DTO。数据不上传服务器,适合对接 JSON 接口时快速生成 C# 模型类。

使用方法

  1. 打开 JSON 转 C# 工具 工具页面
  2. 根据需求输入或粘贴数据到输入区域
  3. 查看实时处理结果,使用复制按钮获取输出

使用场景

  • 对接 REST 接口 — 把接口 JSON 转成 C# 类,配合 HttpClient/RestSharp 做反序列化。
  • EF Core 实体 — 从数据库 JSON 样例生成实体类,用于 Entity Framework Core 映射。
  • Unity 配置建模 — 为 Unity 游戏配置 JSON 生成类型安全的 C# 模型。
  • Azure Function 输入 — 快速生成输入模型类,用于 Azure Function 的 HTTP Trigger。

常见问题

Newtonsoft 和 System.Text.Json 有什么区别?

Newtonsoft.Json 使用 [JsonProperty],System.Text.Json 使用 [JsonPropertyName]。按项目依赖选择。

null 值怎么处理?

null 值推断为 object?,建议用非空样例获得更精确的类型。

nullable 引用类型是什么?

C# 8+ 特性,开启后 string 等引用类型字段会标记为可空(string?),编译器做空值检查。

嵌套对象怎么处理?

每个嵌套对象生成独立的 class,结构清晰可复用。

评论与讨论

还没有评论,来抢沙发吧!

💬 评论仅保存在你的浏览器本地,切换设备后不可见。配置 Giscus 后可支持云端评论。