Home > OS >  C# Deserialize JSON into mulitple/different lists
C# Deserialize JSON into mulitple/different lists

Time:10-11

I have 3 different lists in "UrnikPrevozov" class, I want to populate them with data from JSON for each list, but I am not quite sure how to correctly do it. Any help would be greatly appreciated.

This is my json:

{
  "UrnikAvtobus": [
    {
      "Linija": 34,
      "Trajanje": "01:15:00",
      "Voznik": "Rojko Mulic",
      "Vstop": "Velenje",
      "Izstop": "Maribor",
      "CenaVozovnice": 2.90
    }
  ],
  
  "UrnikAvto": [
    {
      "Znamka": "MERCEDES",
      "Registerska": "LJ 455-AA",
      "Vinjeta": 0,
      "Vstop": "Maribor",
      "Izstop": "Velenje",
      "CenaVozovnice": 5.40
    }
  ],

  "UrnikKombi": [
    {
      "Sedezi": 8,
      "Odhod": "2022-12-12T09:15:00",
      "Prihod": "2022-12-12T12:31:00",
      "Vstop": "Maribor",
      "Izstop": "Ljubljana",
      "CenaVozovnice": 3.70
    }
  ]
}

My UrnikPrevozov class where I want to bind the data to the lists:

public class UrnikPrevozov : Prevoz
{
    public List<AvtobusniPrevoz> UrnikBus { get; set; }

    public List<AvtoPrevoz> UrnikAvto { get; set; }

    public List<KombiPrevoz> UrnikKombi { get; set; }

}

This is how I read my json:

public void LoadJson()
{
    string data = File.ReadAllText("./Data/data.json");

    var urniki = JsonConvert.DeserializeObject<UrnikPrevozov>(data);
}

AvtoPrevoz class

  public class AvtoPrevoz : Prevoz
  {

    public string Znamka { get; set; }
    public string Registerska { get; set; }
    public Vinjeta Vinjeta { get; set; }


    public AvtoPrevoz(string znamka, string registerska, Vinjeta vinjeta, string vstop, string izstop, double cenaVozovnice) : base(vstop, izstop, cenaVozovnice)
    {
      Znamka = znamka;
      Registerska = registerska;
      Vinjeta = vinjeta;
    }
}

Prevoz class

public abstract class Prevoz
{
    public string Vstop { get; set; }
    public string Izstop { get; set; }
    public double CenaVozovnice { get; set; }

    protected Prevoz(string vstop, string izstop, double cenaVozovnice)
    {
        Vstop = vstop;
        Izstop = izstop;
        CenaVozovnice = cenaVozovnice;
    }
}

Method call in main:

UrnikPrevozov urnik = new UrnikPrevozov();
urnik.LoadJson();

CodePudding user response:

Your UrnikPrevozov class should not be inheriting Prevoz since it does not need the fields Prevoz has. And you would need parameterless constructors for all your classes.

Update: Full working code:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
using System.IO;

public class Program
{
  public static void Main()
  {
    string json = File.ReadAllText("./Data/data.json");
    
    UrnikPrevozov urnik = JsonConvert.DeserializeObject<UrnikPrevozov>(json);

    Console.WriteLine(urnik.UrnikAvto.First().Vstop);
  }
}

public class AvtobusniPrevoz : Prevoz
{
  public int Linija { get; set; }
  public string Trajanje { get; set; }
  public string Voznik { get; set; }
}

public class AvtoPrevoz : Prevoz
{
  public string Znamka { get; set; }
  public string Registerska { get; set; }
  public int Vinjeta { get; set; }
}

public class KombiPrevoz : Prevoz
{
  public int Sedezi { get; set; }
  public DateTime Odhod { get; set; }
  public DateTime Prihod { get; set; }
}

public abstract class Prevoz
{
  public string Vstop { get; set; }
  public string Izstop { get; set; }
  public double CenaVozovnice { get; set; }
}

public class UrnikPrevozov
{
  public List<AvtobusniPrevoz> UrnikBus { get; set; }

  public List<AvtoPrevoz> UrnikAvto { get; set; }

  public List<KombiPrevoz> UrnikKombi { get; set; }
}

CodePudding user response:

you have a typo in UrnikPrevozov class , replace UrnikBus by UrnikAvtobus, also you don't need any base class here

public class UrnikPrevozov
{
    public List<AvtobusniPrevoz> UrnikAvtobus { get; set; }

    public List<AvtoPrevoz> UrnikAvto { get; set; }

    public List<KombiPrevoz> UrnikKombi { get; set; }
}
  • Related