canovalik
canovalik

Reputation: 13

C# filtering json results from restclient

I am completely new to C# and have zero background in the language. The only programming language I am an expert in is SQL.

My situation: I have a an API url from a website my company uses to monitor product levels in tanks. The API returns a json list of the tanks (location, tank id, tank size, current level, etc.). My goal is to create a windows form application with an input field. The user types the name of the location, clicks a button, and sees the information for any tanks at that location.

What I have tried:

What I have done so far: Again, I have zero knowledge of programming so do not be critical if I have done things in very unusual/inefficient ways. I have managed to create a windows form that has a button and text box. When the button is clicked, the text box is populated with all of the tank data from the API. I can't figure out how to filter the data that has been returned. My current code is below...

    using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using static APIform.Form1;

namespace APIform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var client = new RestClient("https://{{my tank monitor website}}/admin/data_feed_configs/140.json");
            client.Timeout = -1;
            var request = new RestRequest(Method.GET);
            request.AddHeader("auth-token", "{{my token}}");
            request.AddHeader("auth-email", "{{my authorization email}}");
            request.AddHeader("Authorization", "{{my auth code}}");
            request.AddHeader("Cookie", "{{cookies}}");
            IRestResponse response = client.Execute(request);

            var data = response.Content;
            JArray jsonArray = JArray.Parse(data);

            textBox1.Text = jsonArray.ToString();
        }
    }
}

I have added a model class TankClass that resembles the text returned from the request.

public class TankClass
{
    public string location_name { get; set; }
    public string owner_name { get; set; }
    public string tank_id { get; set; }
    public string tank_name { get; set; }
    public string product_name { get; set; }
    public int tank_size { get; set; }
    public string sensor_value { get; set; }
    public string reading_inches { get; set; }
    public string reading_volume { get; set; }
    public string available_capacity { get; set; }
    public int fill_percentage { get; set; }
    public string fill_status { get; set; }
    public string alert_status { get; set; }
    public int days_to_empty { get; set; }
    public string battery_level { get; set; }
    public string product_sku { get; set; }
    public string reading_time { get; set; }
    public string division { get; set; }
}

Everything I try to deserialize/filter the results does not seem to be working. What do I need to add into the code to make this work? I have a second text box (textbox2) on the form. This is where the user will enter "Warehouse 1" for example. When they then hit the button, I would like only tanks at Warehouse 1 to show in the textbox1 field.

With my current code, this is a sample of what shows in the textbox1 field when clicking the button:

[
  {
    "location_name": "Warehouse 1",
    "owner_name": "ABC Oil, Inc.",
    "tank_id": "W00813862",
    "tank_name": "Dow - Desitherm (TEG) Tank #M-20-065",
    "product_name": "Dow - Desitherm (TEG)",
    "tank_size": 2005.0,
    "sensor_value": "2.379",
    "reading_inches": "29.6",
    "reading_volume": "908.2",
    "available_capacity": "1096.8",
    "fill_percentage": 45,
    "fill_status": "COULD",
    "alert_status": "",
    "days_to_empty": 124.4,
    "battery_level": "6.1",
    "product_sku": "",
    "reading_time": "2020-09-16T10:55:35-04:00",
    "division": "1024"
  },
  {
    "location_name": "Warehouse 2",
    "owner_name": "ABC Oil, Inc.",
    "tank_id": "Z057101",
    "tank_name": "OSI 84  - Diesel Exhaust Fluid (DEF)",
    "product_name": "Diesel Exhaust Fluid (DEF)",
    "tank_size": 8806.0,
    "sensor_value": "2554.0 | 3263.0",
    "reading_inches": "76.3",
    "reading_volume": "4868.8",
    "available_capacity": "3937.2",
    "fill_percentage": 55,
    "fill_status": "GOOD",
    "alert_status": "",
    "days_to_empty": 14.5,
    "battery_level": "-",
    "product_sku": "",
    "reading_time": "2020-09-16T10:59:00-04:00",
    "division": ""
  },
.
.
.
]

My Program.cs tab:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using RestSharp;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Security.Principal;
using System.Text;
using System.IO;
using static APIform.Form1;

namespace APIform
{
    public class TankClass
    {
        public string location_name { get; set; }
        public string owner_name { get; set; }
        public string tank_id { get; set; }
        public string tank_name { get; set; }
        public string product_name { get; set; }
        public double tank_size { get; set; }
        public string sensor_value { get; set; }
        public string reading_inches { get; set; }
        public string reading_volume { get; set; }
        public string available_capacity { get; set; }
        public int fill_percentage { get; set; }
        public string fill_status { get; set; }
        public string alert_status { get; set; }
        public double days_to_empty { get; set; }
        public string battery_level { get; set; }
        public string product_sku { get; set; }
        public DateTime reading_time { get; set; }
        public string division { get; set; }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Upvotes: 1

Views: 823

Answers (1)

jaabh
jaabh

Reputation: 1004

In your class just change tank_size and days_to_empty to a double and reading_time to DateTime.

Then Deserialize into a List<TankClass>

var listOfTanks = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TankClass>>(data);

Then with the form data, you can use Linq to filter the results:

var returnValues = listOfTanks.Where(x => x.location_name == TextBox1.Text);

Upvotes: 2

Related Questions