JSON को C# class में बदलें!

JSON डालें और nested object समेत C# class कोड बनाएं! System.Text.Json [JsonPropertyName], .NET, ASP.NET Core समर्थित।
JSON
परिणाम
 

JSON को C# class में बदलें!

JSON को System.Text.Json के लिए C# classes में बदलें। Property नाम PascalCase में बदले जाते हैं, और हर property पर [JsonPropertyName] attribute होता है ताकि वह ठीक मूल key से bind हो।

यह ASP.NET Core request और response models, बाहरी APIs के HttpClient.GetFromJsonAsync<T>() calls के types, और Unity या MAUI apps की data classes के लिए कॉपी-और-टाइप वाला काम बचाता है।

उपयोग कैसे करें

  1. JSON response या configuration फ़ाइल पेस्ट करें।
  2. टाइप का नाम में root class का नाम डालें (उदाहरण के लिए PaymentResult)।
  3. बनी हुई classes को .cs फ़ाइल में डालें और अपना namespace जोड़ें।
  4. इसे JsonSerializer.Deserialize<PaymentResult>(json) या GetFromJsonAsync<PaymentResult>(url) के साथ इस्तेमाल करें।

उदाहरण

इनपुट (JSON)
{
  "paymentId": "pay_8f2k",
  "amount": 15000,
  "currency": "KRW",
  "approved": true,
  "approvedAt": "2024-05-01T12:30:00Z",
  "card": { "issuer": "VISA", "last4": "4242" },
  "metadata": null
}
आउटपुट (C# · System.Text.Json)
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

public class PaymentResult
{
    [JsonPropertyName("paymentId")]
    public string PaymentId { get; set; } = string.Empty;

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

    [JsonPropertyName("currency")]
    public string Currency { get; set; } = string.Empty;

    [JsonPropertyName("approved")]
    public bool Approved { get; set; }

    [JsonPropertyName("approvedAt")]
    public string ApprovedAt { get; set; } = string.Empty;

    [JsonPropertyName("card")]
    public Card Card { get; set; } = new();

    [JsonPropertyName("metadata")]
    public JsonElement? Metadata { get; set; }
}

public class Card
{
    [JsonPropertyName("issuer")]
    public string Issuer { get; set; } = string.Empty;

    [JsonPropertyName("last4")]
    public string Last4 { get; set; } = string.Empty;
}

हर property पर [JsonPropertyName] है, इसलिए casing नियम चाहे जो हों, paymentId ↔ PaymentId मेल खाते हैं। metadata हमेशा null ही है, इसलिए उसका type अज्ञात है और वह JsonElement? बनता है। String और object properties को string.Empty / new() initializers मिलते हैं, ताकि कोड nullable warnings के बिना compile हो।

समर्थित सुविधाएँ

  • हर property पर [JsonPropertyName("originalKey")], साथ में using System.Text.Json.Serialization;।
  • Types: string, int, 32 bits से बड़े integers के लिए long, decimals के लिए double, bool, और arrays के लिए List<T>।
  • मिले-जुले या अज्ञात type की values JsonElement बनती हैं, और using System.Text.Json; जुड़ता है।
  • कुछ items में न मौजूद या null वाली properties nullable (int?, string?) बनती हैं।
  • Nullable reference warning CS8618 से बचने के लिए required properties को = string.Empty; या = new(); initializers मिलते हैं।
  • C# में property का नाम उसकी class जैसा नहीं हो सकता, इसलिए उसे Value suffix मिलता है (जैसे RootValue)।
  • Top-level array से // Root: List<RootItem> comment और element class बनती है।

उपयोग के मामले

HttpClient से बाहरी APIs call करना

Payment या shipment-tracking API का sample response बदलें और JsonDocument से एक-एक field निकालने की बजाय उसे await http.GetFromJsonAsync<PaymentResult>(url) में type के रूप में इस्तेमाल करें।

ASP.NET Core में webhook payload models

Third-party service आपको जो payload post करती है उससे classes बनाएं और उन्हें [FromBody] parameter के रूप में स्वीकार करें, ताकि model binding और validation हमेशा की तरह काम करें।

appsettings और game data फ़ाइलें

Unity या console app जो JSON data फ़ाइल पढ़ता है, उसकी संरचना classes से बांधने का मतलब है कि data फ़ाइल बदलने पर तुरंत deserialization error दिखेगा।

नोट्स और सीमाएँ

  • Newtonsoft.Json ([JsonProperty]) आउटपुट नहीं बनता। अगर आप Json.NET इस्तेमाल करते हैं, तो attributes को [JsonProperty("key")] में बदलें।
  • Date strings string ही रहती हैं। ISO 8601 values के लिए type को DateTime या DateTimeOffset में बदलना System.Text.Json के parse करने के लिए काफ़ी है।
  • Target-typed new() और string? मानते हैं कि C# 9+ और nullable context चालू है (.NET 6 से default)।
  • record या required आउटपुट नहीं है। Immutable models के लिए { get; set; } को { get; init; } में बदलें।
  • सभी classes एक block में आती हैं; फ़ाइलें और namespaces खुद बांटें।

अक्सर पूछे जाने वाले प्रश्न

Key पहले से मेल खाती हो तब भी [JsonPropertyName] क्यों जोड़ें?

System.Text.Json default रूप से case-sensitive है। Custom JsonSerializerOptions के बिना console या worker projects में paymentId PaymentId से bind नहीं होगा, इसलिए configuration चाहे जो हो, काम करने के लिए attribute हमेशा लिखा जाता है।

JsonElement? property कैसे पढ़ूं?

ValueKind जांचें, फिर GetString(), GetInt32() या Deserialize<T>() इस्तेमाल करें। असली type पता चलने पर JsonElement को उससे बदलना ज़्यादा साफ़ विकल्प है।

क्या camelCase property नाम रख सकते हैं?

आउटपुट C# PascalCase convention का पालन करता है। मूल key [JsonPropertyName] में रहती है, इसलिए जब तक attribute बना रहे, आप properties को आज़ादी से rename कर सकते हैं।

क्या यह Unity के JsonUtility के साथ काम करता है?

JsonUtility सिर्फ़ [Serializable] classes के public fields संभालता है, properties नहीं, और [JsonPropertyName] को अनदेखा करता है। Unity में आउटपुट को Newtonsoft.Json package या System.Text.Json के साथ इस्तेमाल करें।