C# - Deserialize JSON to List of Employee Objects

Поделиться
HTML-код
  • Опубликовано: 19 окт 2024

Комментарии • 14

  • @liquidoxygenspiritsgenuine7786
    @liquidoxygenspiritsgenuine7786 2 года назад +1

    I invested around 3 hours searching the google for any website or youtube VDO that would demonstrate how to read and store the "full json" as a variable and as well as store "individual elements" of a json to a model property...
    and, after my 3 hours of super searching on the net, I found your video really really AWESOME. it is EXTREMELY simple to understand and very nicely demonstrated.
    Please continue more of it.. and if the teachings and trainings are equally good, all viewers can also arrange for funding to facilitate this benefit.

  • @marioguitarify
    @marioguitarify 2 года назад

    Nice tutorial. Keep going!

  • @OrtonLongGaming
    @OrtonLongGaming 2 года назад +1

    How would i request (for example) the email belonging to a certain employee?

    • @MaxLevelCode
      @MaxLevelCode  2 года назад

      After you have the object list of employees, you can use LINQ to query the list.
      For example:
      var emp = emps.Where(currEmp => currEmp.EmpId == 1).FirstOrDefault();
      Then you can access the specific employee's email using:
      emp.Email

  • @royianchowdhury9350
    @royianchowdhury9350 2 года назад

    thank you!

  • @joadm5724
    @joadm5724 Год назад

    Hello, is there a way to deserialize a List of objects that are derived from an abstract class?
    i.e. I have an object with a List, and Car, Motorcycle and Bicycle inherited from Vehicle.
    How can i deserialize this?
    {
    "vehicles": [
    {
    "type": "car",
    "numberOfWheels": 4,
    "numberOfDoors": 5
    },
    {
    "type": "bicycle",
    "numberOfWheels": 2
    "color": "red",
    }
    ]
    }
    if in my API endpoint i ask for ([FromBody] ObjectWithListOfVehicles object) it'll throw an exception saying that I cannot instantiate an abstract class, but in reality it's a list of derived objects.

  • @michiel4697
    @michiel4697 2 года назад

    thx

  • @rodrigodasilvalima6927
    @rodrigodasilvalima6927 2 года назад

    I loved this video, but how i can make the same process with values of the type 'int' in my json file.

    • @MaxLevelCode
      @MaxLevelCode  2 года назад

      For the int values don't use double quotes in the JSON file and in your class declare the property data type as int.
      {
      "IntProperty": 100
      }

    • @rodrigodasilvalima6927
      @rodrigodasilvalima6927 2 года назад

      @@MaxLevelCode Alright man, thanks for your answer

  • @aashishmalviya473
    @aashishmalviya473 8 месяцев назад

    How to serialize a string read from text file in c#

  • @郑天-f9i
    @郑天-f9i 2 года назад

    hi, thanks for your video, but i found a bug when I using your code,
    why when i write" var glove50s = JsonSerializer.Deserialize(jsonData1);" (jsonData1 is the String read from file), it shows an error on jsonData1 said "cannot convert from String to newtonsoft.Json.JsonReader"

    • @MaxLevelCode
      @MaxLevelCode  2 года назад

      The error message you are getting is a Newtonsoft exception but in my video, I'm using System.Text.Json, not newtonsoft. You must have a reference to the Newtonsoft NuGet package in your project which is not needed. Try creating a new project and not adding a reference to Newtonsoft and it will work. Otherwise, use the fully qualified namespace System.Text Json.

    • @doganvolkan819
      @doganvolkan819 Год назад

      @@MaxLevelCode i have to use using Newtonsoft.Json