JSON 輸入
生成的 C#
輸入 JSON 資料,為你的 .NET 專案產生強型別 C# 類別與 records
類別、records、DTO 與具有 nullable reference types 的屬性
從 JSON 為 .NET 與 ASP.NET Core 產生強型別 C# 類別、record 與 DTO 模型
輸入 JSON 資料,為你的 .NET 專案產生強型別 C# 類別與 records
類別、records、DTO 與具有 nullable reference types 的屬性
使用此 JSON 轉 C# 產生器,可從 JSON 範例產生 C# class 或 record,適合 ASP.NET Core 與 .NET 應用。
步驟 1 – 貼上 JSON 範例
匯入 從檔案、URL 或示例資料載入 JSON。步驟 2 – 選擇 C# 產生選項
Class Name 與 Namespace,與專案結構一致。System.Text.Json 或 Newtonsoft.Json)。string? 等型別。步驟 3 – 檢查產生的程式碼
根型別名稱、null 處理策略與可選的框架選項。步驟 4 – 在 .NET 中使用 DTO
步驟 5 – 複製或下載
快速提示
[JsonPropertyName]/[JsonProperty] 進行對應。// JSON 輸入
{
"id": 123,
"name": "Maeve Winters",
"email": "[email protected]",
"active": true,
"roles": ["admin", "editor"],
"metadata": { "plan": "pro" },
"createdAt": "2024-03-01T10:15:00Z",
"score": 99.5,
"notes": null
}
// 產生的 C# DTO(簡化)
public class Metadata
{
public string Plan { get; set; } = string.Empty;
}
public class Root
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Email { get; set; }
public bool Active { get; set; }
public List<string> Roles { get; set; } = new();
public Metadata Metadata { get; set; } = new();
public DateTime CreatedAt { get; set; }
public double Score { get; set; }
public object? Notes { get; set; }
}探索更多可與本 JSON 轉 C# 產生器搭配使用的 JSON 與 Schema 工具。
產生器會建立包含屬性的 C# 類別與 C# 9+ records,支援 JSON.NET 與 System.Text.Json 屬性,並遵循具有適當命名空間的現代 .NET 命名慣例。
可以!選擇「Record」作為框架可產生具有不可變屬性的現代 C# records,非常適合資料傳輸物件(DTO)、API 契約與值型別。
產生器支援 Newtonsoft.Json 與 System.Text.Json 屬性,可使用自訂屬性名稱、null 處理與彈性設定,正確進行 JSON 序列化與反序列化。
產生器會為可能為 null 的屬性加入可為 null 註記(object?),遵循現代 C# nullable reference types 慣例,讓安全對應可選 JSON 欄位更容易。
可以。JSON 轉 C# 程式碼適用於 ASP.NET Core 控制器、Minimal API、Blazor 元件,以及任何需要 JSON payload 強型別模型的 .NET 應用程式。
是的,JSON Swiss 的 JSON 轉 C# 產生器可在瀏覽器中免費使用。你可以貼上 JSON、設定選項並產生 C# 程式碼,無需安裝工具或擴充套件。