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 के लिए कॉपी-और-टाइप वाला काम बचाता है।
टाइप का नाम में root class का नाम डालें (उदाहरण के लिए PaymentResult)।.cs फ़ाइल में डालें और अपना namespace जोड़ें।JsonSerializer.Deserialize<PaymentResult>(json) या GetFromJsonAsync<PaymentResult>(url) के साथ इस्तेमाल करें।{
"paymentId": "pay_8f2k",
"amount": 15000,
"currency": "KRW",
"approved": true,
"approvedAt": "2024-05-01T12:30:00Z",
"card": { "issuer": "VISA", "last4": "4242" },
"metadata": null
}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 हो।
[JsonPropertyName("originalKey")], साथ में using System.Text.Json.Serialization;।string, int, 32 bits से बड़े integers के लिए long, decimals के लिए double, bool, और arrays के लिए List<T>।JsonElement बनती हैं, और using System.Text.Json; जुड़ता है।null वाली properties nullable (int?, string?) बनती हैं।= string.Empty; या = new(); initializers मिलते हैं।Value suffix मिलता है (जैसे RootValue)।// Root: List<RootItem> comment और element class बनती है।Payment या shipment-tracking API का sample response बदलें और JsonDocument से एक-एक field निकालने की बजाय उसे await http.GetFromJsonAsync<PaymentResult>(url) में type के रूप में इस्तेमाल करें।
Third-party service आपको जो payload post करती है उससे classes बनाएं और उन्हें [FromBody] parameter के रूप में स्वीकार करें, ताकि model binding और validation हमेशा की तरह काम करें।
Unity या console app जो JSON data फ़ाइल पढ़ता है, उसकी संरचना classes से बांधने का मतलब है कि data फ़ाइल बदलने पर तुरंत deserialization error दिखेगा।
[JsonProperty]) आउटपुट नहीं बनता। अगर आप Json.NET इस्तेमाल करते हैं, तो attributes को [JsonProperty("key")] में बदलें।string ही रहती हैं। ISO 8601 values के लिए type को DateTime या DateTimeOffset में बदलना System.Text.Json के parse करने के लिए काफ़ी है।new() और string? मानते हैं कि C# 9+ और nullable context चालू है (.NET 6 से default)।record या required आउटपुट नहीं है। Immutable models के लिए { get; set; } को { get; init; } में बदलें।[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 को उससे बदलना ज़्यादा साफ़ विकल्प है।
आउटपुट C# PascalCase convention का पालन करता है। मूल key [JsonPropertyName] में रहती है, इसलिए जब तक attribute बना रहे, आप properties को आज़ादी से rename कर सकते हैं।
JsonUtility सिर्फ़ [Serializable] classes के public fields संभालता है, properties नहीं, और [JsonPropertyName] को अनदेखा करता है। Unity में आउटपुट को Newtonsoft.Json package या System.Text.Json के साथ इस्तेमाल करें।