I have this JSON result that i recieve from API is there a simple way to fetch the needed values from the response ? What is needed is only these values AssetName, SystemID, Lat, Lng and Timestmp into a custom Class with name Result
the resultJson looks like the following
{
"result": [
{
"Asset": {
"AssetName": "Banner",
"AssetType": "Medic",
"AssetCategory": "Personnel",
"Hardware": {
"HardwareName": " Tag 88",
"HardwareType": "Personnel Tag",
"HardwareFamily": " Group 2",
"DeviceType": "Location Tag",
"SystemId": "8845"
}
},
"DetectingAsset": {
"AssetName": "Shop 2 TR",
"AssetType": "Tag Reader",
"AssetCategory": "Network Access Point",
"Hardware": {
"HardwareName": "2 TR",
"HardwareType": "Tag Reader",
"HardwareFamily": "Group 2",
"DeviceType": "Tag Reader",
"SystemId": "9.9.1"
}
},
"Location": {
"Maps": [
"Hard Rock"
],
"Zone": "Machine Shop",
"Lat": null,
"Lng": null
},
"Timestamp": "2023-05-05T11:07:45.673"
},
{
"Asset": {
"AssetName": "Test1",
"AssetType": "Medic",
"AssetCategory": "Personnel",
"Hardware": {
"HardwareName": "Tag 88",
"HardwareType": "Personnel Tag",
"HardwareFamily": "Group ABIP",
"DeviceType": "Location Tag",
"SystemId": "1022"
}
},
"DetectingAsset": {
"AssetName": "Machine Shop 2 TR",
"AssetType": "Tag Reader",
"AssetCategory": "Network Access Point",
"Hardware": {
"HardwareName": "2 TR",
"HardwareType": "Tag Reader",
"HardwareFamily": "Group AIP",
"DeviceType": "Tag Reader",
"SystemId": "9.9.1"
}
},
"Location": {
"Maps": [
"Hard Rock"
],
"Zone": "Machine Shop",
"Lat": null,
"Lng": null
},
"Timestamp": "2023-05-05T11:07:45.673"
}
]
}
The Classes i've created is the following
class Result
{
public APIResult[] aPIResult { get; set; }
}
class Result
{
public string TimeStamp { get; set; }
public string AssetName { get; set; }
public string locationLat { get; set; }
public string locationLng { get; set; }
public string SystemId {get;set;}
}
you can use the JObject
class from the Newtonsoft.Json
(Json.NET) library to parse and extract the needed values from the JSON response.
using Newtonsoft.Json.Linq;
using System;
class Program
{
static void Main()
{
string json = "Your JSON string";
JObject jsonObject = JObject.Parse(json);
JArray resultsArray = (JArray)jsonObject["result"];
foreach (JToken resultToken in resultsArray)
{
Result result = new Result();
result.AssetName = (string)resultToken["Asset"]["AssetName"];
result.SystemId = (string)resultToken["Asset"]["Hardware"]["SystemId"];
result.locationLat = (string)resultToken["Location"]["Lat"];
result.locationLng = (string)resultToken["Location"]["Lng"];
result.TimeStamp = (string)resultToken["Timestamp"];
Console.WriteLine($"AssetName: {result.AssetName}");
Console.WriteLine($"SystemID: {result.SystemId}");
Console.WriteLine($"Lat: {result.locationLat}");
Console.WriteLine($"Lng: {result.locationLng}");
Console.WriteLine($"Timestmp: {result.TimeStamp}");
Console.WriteLine();
}
}
}
class Result
{
public string TimeStamp { get; set; }
public string AssetName { get; set; }
public string locationLat { get; set; }
public string locationLng { get; set; }
public string SystemId { get; set; }
}