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;}
}
4
Answers
you can use the
JObject
class from theNewtonsoft.Json
(Json.NET) library to parse and extract the needed values from the JSON response.You can use Newtonsoft.Json.JsonConvert
You can use System.Text.Json for this (installable via Nuget Package Manager in Visual Studio):
you can try this code