Home > database >  how to access to a class list deserialized from a json file
how to access to a class list deserialized from a json file

Time:08-25

I've been trying to get data from a JSON file with VScode. I made some classes and can use some of the values saved in the different classes. My problem right now is, that I got a Class which contains a List within a List... How can I get this value? Got some Code-example. Thank you in advance for your help:

namespace TestDeserialize
{
    public class DataInfo
    {
        [JsonProperty("Sample rate")]
        public int SampleRate { get; set; }
    }

    public class Datum
    {
        public string Trigger { get; set; }

        [JsonProperty("Channel :AS_V[mm]")]
        public List<List<double>> ChannelASVMm { get; set; } //Want to get those values
    }

    public class Root
    {
        public DataInfo Data_info { get; set; }
        public List<Datum> Data { get; set; }
    }



    public class Program
    {
        public static void Main()
        {
            string filename2 = @"C:\Users\10606167\TestDatenAbstand.json";
            Root datum = JsonConvert.DeserializeObject<Root>(File.ReadAllText(filename2));
            Console.WriteLine($"Samplerate: {datum.Data_info.SampleRate}");    
        }
    }
}

This is my JSON-File:

{"Data_info":{"File name":"C:\\Dewesoft\\Data\\Test(2).dxd","Start time":"22.08.2022","Number of channels":1,"Sample rate":100,"Reduced rate":10},
"Events":[{"Event Type:":1,"Event :":"Speicherung gestartet um","Time:":0,"Comment:":""},{"Event Type:":2,"Event :":"Speicherung gestoppt um","Time:":1.72,"Comment:":""}]
, "Data":[
{
"Trigger" : "Data1",
"Channel :AS_V[mm]":[
[0, 1.57027852535248]
]
}]}

I can get the value of my samplerate but I dont know how to get the value of ChannelASVm...

CodePudding user response:

If you simply want the first value of the first List of the first Datum in your Root class, you just have to do something like this :

public class Program
{
    public static void Main()
    {
        string filename2 = @"C:\Users\10606167\TestDatenAbstand.json";
        Root datum = JsonConvert.DeserializeObject<Root>(File.ReadAllText(filename2));
        
        double valueInListOfList = datum.Data[0].ChannelASVMm[0][0];
    }
}

To target an element in a List inside another List by using indexes, you just have to chain [] like this : [<indexOfFirstList>][<indexOfSecondList>]

If you want to target specific Datum instances and specific double values in ChannelASVm, you will have to add some conditions to be able to target exactly what instances/values you want.

For that you can take a look at some Linq functions :

Or simply do a foreach and extract the data you need, but I recommend to use Linq instead

CodePudding user response:

if you need just Channel :AS_V[mm], you don't need any classes

List<double> channel = JObject.Parse(json)["Data"]
          .SelectMany(x => x["Channel :AS_V[mm]"]).First().ToObject<List<Double>>();

or you can use classes

List<List<double>> channelASVMm = datum.Data.SelectMany(x=> x.ChannelASVMm).ToList();
List<double> channelASVMmFirst = channelASVMm.FirstOrDefault();
  • Related