JSON to C# Class
Convert JSON objects into C# class definitions with System.Text.Json decorators.
using System.Text.Json.Serialization;
using System.Collections.Generic;
public class Root
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("username")]
public string Username { get; set; }
[JsonPropertyName("email")]
public string Email { get; set; }
[JsonPropertyName("address")]
public Address Address { get; set; }
[JsonPropertyName("phone")]
public string Phone { get; set; }
[JsonPropertyName("website")]
public string Website { get; set; }
[JsonPropertyName("company")]
public Company Company { get; set; }
}
public class Address
{
[JsonPropertyName("street")]
public string Street { get; set; }
[JsonPropertyName("suite")]
public string Suite { get; set; }
[JsonPropertyName("city")]
public string City { get; set; }
[JsonPropertyName("zipcode")]
public string Zipcode { get; set; }
[JsonPropertyName("geo")]
public Geo Geo { get; set; }
}
public class Geo
{
[JsonPropertyName("lat")]
public string Lat { get; set; }
[JsonPropertyName("lng")]
public string Lng { get; set; }
}
public class Company
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("catchPhrase")]
public string Catchphrase { get; set; }
[JsonPropertyName("bs")]
public string Bs { get; set; }
}
PascalCase Properties
Automatically converts JSON keys to standard C# PascalCase naming conventions for properties.
System.Text.Json
Includes JsonPropertyName attributes to maintain mapping with original JSON keys during serialization.