Click here to Skip to main content
15,902,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...
C#
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.
C#
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:
C#
[
  {
    "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": ""
  },
.
.
.
]
Posted
Updated 17-Sep-20 5:35am
v3

1 solution

You need to deserialize the JSON object into a defined C# object.

Reference: How to serialize and deserialize JSON using C# - .NET | Microsoft Docs[^]

Example:
C#
public class WeatherForecast
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string Summary { get; set; }
}

// get json string from file or http response
jsonString = File.ReadAllText(fileName);
weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString);

// now, use json data like a normal c# object
string summary = weatherForecast.Summary;


In your case, create a model class for the JSON response. Use that class as type for deserialization and then use it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900