JSON から C# クラスへ
JSON を System.Text.Json または Newtonsoft.Json 用の C# クラスまたはレコードに変換。null 許容型、JsonPropertyName 属性、DateTimeOffset/Guid の検出に対応します。
For strings that look like dates and UUIDs
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class Root
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("username")]
public string Username { get; set; } = string.Empty;
[JsonPropertyName("email")]
public string Email { get; set; } = string.Empty;
[JsonPropertyName("isActive")]
public bool IsActive { get; set; }
[JsonPropertyName("rating")]
public double Rating { get; set; }
[JsonPropertyName("createdAt")]
public DateTimeOffset CreatedAt { get; set; }
[JsonPropertyName("avatarUrl")]
public object? AvatarUrl { get; set; }
[JsonPropertyName("tags")]
public List<string> Tags { get; set; } = new();
[JsonPropertyName("address")]
public Address Address { get; set; } = new();
[JsonPropertyName("orders")]
public List<Order> Orders { get; set; } = new();
}
public class Address
{
[JsonPropertyName("street")]
public string Street { get; set; } = string.Empty;
[JsonPropertyName("city")]
public string City { get; set; } = string.Empty;
[JsonPropertyName("zipCode")]
public string ZipCode { get; set; } = string.Empty;
}
public class Order
{
[JsonPropertyName("orderId")]
public string OrderId { get; set; } = string.Empty;
[JsonPropertyName("total")]
public double Total { get; set; }
[JsonPropertyName("items")]
public int Items { get; set; }
[JsonPropertyName("shipped")]
public bool Shipped { get; set; }
[JsonPropertyName("coupon")]
public string? Coupon { get; set; }
}
Arrays of objects are merged: a key missing from some items becomes optional, null makes a field nullable, and identical shapes share one type. Paste several examples in an array for the most accurate types.
このツールについて
JSON を貼り付けると、System.Text.Json または Newtonsoft.Json ですぐに使える C# のクラスやレコードが得られます。プロパティは PascalCase で、[JsonPropertyName] または [JsonProperty] 属性が付くため、すべてのキーが正確に対応付けられます。null 許容参照型も正しく扱われ、省略可能な値や null の値は null 許容になり、null でない文字列、リスト、オブジェクトには初期値が設定されるので、コンパイラーが CS8618 警告を出すことはありません。整数は値に応じて int または long に、小数は double になり、タイムスタンプ、日付、UUID のような文字列は DateTimeOffset、DateOnly、Guid にできます。ネストしたオブジェクトは独自のクラスになり、リストは List<T> に、ID をキーにしたオブジェクトは Dictionary<string, T> になります。ファイルスコープの名前空間を選べるほか、同じページから Go、Rust、Python、Kotlin に切り替えることもできます。
JSON を C# クラスに変換する方法
JSON サンプルから C# の型を生成します。
- 1
JSON を貼り付け
JSON オブジェクト、または例の配列を貼り付けます。
- 2
オプションを選ぶ
System.Text.Json か Newtonsoft、クラスかレコード、名前空間を選びます。
- 3
コードをコピー
クラスをコピーするか、Types.cs をダウンロードします。