📘 C# Blog Series – Blog 23 **“C# JSON Handling — JSON को Read/Write करके Modern APIs कैसे बनाते हैं?

📘 C# Blog Series – Blog 23

**“C# JSON Handling — JSON को Read/Write करके Modern APIs कैसे बनाते हैं?





(Real-Life Example: GPS Live API JSON Output)”**

आज की दुनिया में हर app, हर server, हर API JSON पर चलता है
चाहे GPS server हो, mobile app backend, billing, e-commerce, school ERP —
JSON everywhere.

C# में JSON बहुत आसानी से handle किया जा सकता है:

  • Parse JSON

  • Convert object → JSON

  • JSON → object

  • GPS live data API बनाना

  • Device logs render करना

  • WebSocket updates भेजना

आज हम इसे real-life GPS use-case के साथ समझेंगे।


🟦 1. JSON क्या होता है? (Super Simple)

JSON =
Data को readable और simple format में भेजने का तरीका।

Example:

{ "IMEI": "861128068064267", "Latitude": 26.85, "Longitude": 80.94, "Battery": 85 }

API में यही format use होता है।


🟢 2. C# में JSON Handling कैसे करते हैं?

Most commonly used libraries:

System.Text.Json (Built-in, fast)

Newtonsoft.Json (Most popular & powerful)

हम दोनों देखते हैं।


3. Object → JSON (Serialize)

Using System.Text.Json

var json = JsonSerializer.Serialize(device);

Using Newtonsoft.Json

var json = JsonConvert.SerializeObject(device);

अब json एक string है।


4. JSON → Object (Deserialize)

var device = JsonSerializer.Deserialize<GPSDevice>(jsonString);

Newtonsoft:

var device = JsonConvert.DeserializeObject<GPSDevice>(jsonString);

🌟 5. Real-Life GPS Example — API में live device data देना

Model:

class GPSDevice { public string IMEI { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public int Battery { get; set; } }

API Output JSON:

[HttpGet("device/{imei}")] public IActionResult GetDevice(string imei) { var device = Devices[imei]; return Ok(device); // auto JSON }

ASP.NET Core automatically JSON output देता है!

Output:

{ "imei": "861128068064267", "latitude": 26.85, "longitude": 80.94, "battery": 86 }

6. GPS Packet से JSON Convert (Parsing + JSON)

Example packet:

G17,IMEI=861128068064267,LAT=26.85,LON=80.94,BAT=86

Convert to JSON:

var data = new GPSDevice { IMEI = imei, Latitude = lat, Longitude = lon, Battery = battery }; string json = JsonSerializer.Serialize(data);

अब आप यह JSON:

  • WebSocket clients को भेज सकते हो

  • API output में दे सकते हो

  • File में save कर सकते हो


🟣 7. GPS Server में JSON का सबसे बड़ा Use: Live Tracking Map

Your WebSocket code:

var json = JsonSerializer.Serialize(device); await ws.SendAsync(json);

Frontend map को live data मिलता है:

{ "imei": "861128068064267", "lat": 26.85, "lon": 80.94, "speed": 45 }

🟠 8. JSON Beautify (Pretty Print)

var json = JsonSerializer.Serialize(device, new JsonSerializerOptions { WriteIndented = true });

Output human-readable होगा।


🔵 9. JSON Array Handling (List → JSON)

Devices की list JSON में:

string json = JsonConvert.SerializeObject(devices);

Output:

[ { "imei": "8611...", "lat": 26.8, ... }, { "imei": "8611...", "lat": 26.9, ... } ]

Perfect for dashboard maps.


🟤 10. JSON File Handling (Save Logs as JSON)

File.AppendAllText("gps.json", json + ",\n");

Logs now structured → easy to analyze.


🔐 11. Error Handling

try { var obj = JsonSerializer.Deserialize<GPSDevice>(json); } catch (JsonException ex) { Console.WriteLine("Invalid JSON: " + ex.Message); }

📌 12. Newtonsoft Special Features (Advanced):

✔ Ignore null
✔ Custom converters
✔ Conditional serialization
✔ Complex objects
✔ Polymorphic JSON

Big systems में Newtonsoft ज़्यादा use होता है।


🔚 Conclusion

C# JSON handling modern APIs का backbone है:

✔ Clean API output
✔ WebSocket updates
✔ GPS live map
✔ Logs save करना
✔ Device status share करना
✔ Mobile apps से communicate करना

Without JSON → कोई भी modern system चल ही नहीं सकता।

Post a Comment

0 Comments

Translate

Close Menu