(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 के साथ समझेंगे।
JSON =
Data को readable और simple format में भेजने का तरीका।
Example:
{
"IMEI": "861128068064267",
"Latitude": 26.85,
"Longitude": 80.94,
"Battery": 85
}
API में यही format use होता है।
Most commonly used libraries:
System.Text.Json (Built-in, fast)Newtonsoft.Json (Most popular & powerful)हम दोनों देखते हैं।
var json = JsonSerializer.Serialize(device);
var json = JsonConvert.SerializeObject(device);
अब json एक string है।
var device = JsonSerializer.Deserialize<GPSDevice>(jsonString);
Newtonsoft:
var device = JsonConvert.DeserializeObject<GPSDevice>(jsonString);
class GPSDevice
{
public string IMEI { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public int Battery { get; set; }
}
[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
}
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 कर सकते हो
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
}
var json = JsonSerializer.Serialize(device,
new JsonSerializerOptions { WriteIndented = true });
Output human-readable होगा।
Devices की list JSON में:
string json = JsonConvert.SerializeObject(devices);
Output:
[
{ "imei": "8611...", "lat": 26.8, ... },
{ "imei": "8611...", "lat": 26.9, ... }
]
Perfect for dashboard maps.
File.AppendAllText("gps.json", json + ",\n");
Logs now structured → easy to analyze.
try
{
var obj = JsonSerializer.Deserialize<GPSDevice>(json);
}
catch (JsonException ex)
{
Console.WriteLine("Invalid JSON: " + ex.Message);
}
✔ Ignore null
✔ Custom converters
✔ Conditional serialization
✔ Complex objects
✔ Polymorphic JSON
Big systems में Newtonsoft ज़्यादा use होता है।
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 चल ही नहीं सकता।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav