skip to Main Content

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


  1. 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; }
    }
    
    Login or Signup to reply.
  2. You can use Newtonsoft.Json.JsonConvert

    ObjectType object = JsonConvert.DeserializeObject<ObjectType>(jsonString);
    
    Login or Signup to reply.
  3. You can use System.Text.Json for this (installable via Nuget Package Manager in Visual Studio):

    Result result = JsonSerializer.Deserialize<Result>(jsonDataString);
    
    Login or Signup to reply.
  4. you can try this code

     Data data = JsonConvert.DeserializeObject<Data>(json);
    
    public class Data
    {
        public List<Result> result { get; set; }
    }
    
    public class Result
    {
        public Asset Asset { get; set; }
        public Asset DetectingAsset { get; set; }
        public Location Location { get; set; }
        public DateTime Timestamp { get; set; }
    }
    
    public class Asset
    {
        public string AssetName { get; set; }
        public Hardware Hardware { get; set; }
    }
    
    public class Hardware
    {
        public string SystemId { get; set; }
    }
    
    public class Location
    {
        public object Lat { get; set; }
        public object Lng { get; set; }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search