在线检测
JSON工具
编码/加密
图片处理
文字处理
格式化
网络
前端
后端
搜索工具、优惠码…⌘K

JSON 转 C# 实体类

粘贴 JSON,自动推断类型递归生成嵌套类PascalCase 属性,注解可选 System.Text.Json / Newtonsoft / 无。纯前端运行、不上传。

JSON 输入
C# 实体类
using System.Collections.Generic;
using System.Text.Json.Serialization;

public class Root
{
    [JsonPropertyName("id")]
    public int Id { get; set; }

    [JsonPropertyName("user_name")]
    public string UserName { get; set; }

    [JsonPropertyName("active")]
    public bool Active { get; set; }

    [JsonPropertyName("balance")]
    public double Balance { get; set; }

    [JsonPropertyName("roles")]
    public List<string> Roles { get; set; }

    [JsonPropertyName("profile")]
    public Profile Profile { get; set; }

    [JsonPropertyName("orders")]
    public List<Order> Orders { get; set; }
}

public class Profile
{
    [JsonPropertyName("nick_name")]
    public string NickName { get; set; }

    [JsonPropertyName("age")]
    public int Age { get; set; }
}

public class Order
{
    [JsonPropertyName("order_no")]
    public string OrderNo { get; set; }

    [JsonPropertyName("amount")]
    public int Amount { get; set; }
}

怎么用?

  • 左侧粘贴 JSON(对象或对象数组,数组取第一个元素推断结构)。
  • 右侧实时生成 C# class,点「复制」拿走全部代码。
  • 改「根类名」决定最外层类名;嵌套对象按字段名生成子类。
  • 切换注解风格:System.Text.Json(内置,推荐)/ Newtonsoft / 无注解。

为什么要加 [JsonPropertyName] 注解?

C# 属性用 PascalCase(UserName),而 JSON 字段常是 camelCase / snake_case (userName / user_name)。两者不一致时,反序列化默认对不上, 必须靠 [JsonPropertyName("user_name")](或 Newtonsoft 的 [JsonProperty]) 建立映射。本工具在名称不一致时自动补上。

类型推断的边界

推断基于样本值:整数默认 int、超范围用 long,小数用 double。 但业务上金额建议手动改 decimal(避免浮点误差)、 时间字段建议改 DateTimenull 无法推断类型,会降级成 object,需你按实际补类型。

关于 chdh.me 在线工具

本工具是 出海导航 chdh.me 免费在线工具集的后端方向一员, 同类还有 JSON 转 Java 实体类SQL 转 C#JSON 转 TypeScript。 均纯前端运行、不上传、不追踪