skip to Main Content

I have a JSON file with the following structure:

{
    "name": "ML4 093 Historical",
    "unit": "pounds",
    "data":[
        {
            "date": "1985-12-01",
            "value": "250"
        },
        {
            "date": "1985-11-01",
            "value": "237"
        },
        {
            "date": "1985-10-01",
            "value": "286"
        }
    ]
}

Long story short, I need to get the value that corresponds to a specific date (for example, 250 for 1985-12-01 or 237 for 1985-11-01).

I understand how to get the information for a single element (for example, getting pounds for unit), but I have no clue how to go about doing it in this case.

This is what I have to get the information for a single element:

using System.Text.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test
{

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


        private void Form1_Load(object sender, EventArgs e)
        {
             Label unitname = label3;

            string QUERY_URL = "JSON URL HERE";
                Uri queryUri = new Uri(QUERY_URL);

            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                  var json_data = client.DownloadString(queryUri);

                  var jsonDocument = JsonDocument.Parse(json_data);

                  var rootElement = jsonDocument.RootElement;

                  var unitElement = rootElement.GetProperty("unit");

                  var unit = unitElement.GetString();

                  unitname.Text = unit;
            }

        }
    }
}

JSON URL HERE is, obviously, replaced with the actual URL in the code – I just can’t share it publicly.

2

Answers


  1. You can create a class representing this JSON object (keeping in mind that Data should be an entirely separate object, inside the parent object), like so:

        public class DataObject
        {
            public string date { get; set; }
            public string value { get; set; }
        }
    
        public class FormObject
        {
            public string name { get; set; }
            public string unit { get; set; }
            public List<Data> data { get; set; }
        }
    

    Then to retrieve the value, an example would be:

    var rootElement = (DataObject)jsonDocument.RootElement;  //there are other better ways to do this - you might want to take a look at Automapper library
    var unitElement = rootElement.Data[1].Value  //to get the first data value element. 
       
    
    Login or Signup to reply.
  2. You can create a model class

    DataModel.cs

    using System.Collections.Generic;
    
    namespace MyFoodBag.Forecast.Presentation.Forecaster.Models
    {
        public class DataModel
        {
            public string Name { get; set; }
            public string Unit { get; set; }
            public List<Data> Data { get; set; }
        }
    
        public class Data
        {
            public string Date { get; set; }
            public string Value { get; set; }
        }
    }
    

    After that just a little modification in you code should work

    using System.Text.Json;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace test
    {
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                 Label unitname = label3;
    
                string QUERY_URL = "JSON URL HERE";
                Uri queryUri = new Uri(QUERY_URL);
    
                using (System.Net.WebClient client = new System.Net.WebClient())
                {
                      var json_data = client.DownloadString(queryUri);
                      // map json to model object
                      var dataModel= JsonConvert.DeserializeObject<DataModel>(json_data);
    
                      var valueForDate = dataModel.Data.Where(x => x.Date == <whateverdate>).Select(v => v.Value);
                }
    
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search