How to Deserialize JSON Nested Arrays into C# using Newtonsoft.

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

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

  • @SoftwareNuggets
    @SoftwareNuggets  2 года назад +2

    C# code for project
    /***************** main.cs **********************/
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;
    using Newtonsoft.Json.Converters;
    using Newtonsoft.Json.Linq;
    using System.IO;
    namespace ModelThatJSON
    {
    class Program
    {
    static void Main(string[] args)
    {
    string fileName =
    @"C:\tmp\arrays.json";
    string jsonText = File.ReadAllText(fileName);
    var data = Newtonsoft.Json.
    JsonConvert.DeserializeObject(jsonText);
    }
    }
    }
    /*************** whip.cs *********************/
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ModelThatJSON
    {
    public class Whip
    {
    public vehicle[] cars { get; set; }
    public vehicle[] trucks { get; set; }
    }
    public class vehicle
    {
    public string brand { get; set; }
    public _model[] models { get; set; }
    }
    public class _model
    {
    public string model { get; set; }
    public int? basePrice { get; set; }
    public string[] colors { get; set; }
    }
    }
    /***********
    {
    "cars": [
    {
    "brand": "Toyo",
    "models": [
    {
    "model": "car_model_1",
    "basePrice": 18000,
    "colors":
    [
    "red",
    "white",
    "blue"
    ]
    },
    {
    "model": "car_model_2",
    "colors":
    [
    "red",
    "white",
    "blue",
    "gray",
    "sky"
    ]
    },
    {
    "model": "car_model_3",
    "colors":
    [
    "red",
    "white",
    "blue",
    "green"
    ]
    }
    ]
    },
    {
    "brand": "Hoyo",
    "models": [
    {
    "model": "car_model_hoy_1",
    "basePrice": 18000,
    "colors":
    [
    "red",
    "white",
    "blue"
    ]
    },
    {
    "model": "car_model_hoy_2",
    "colors":
    [
    "red",
    "white",
    "blue",
    "yellow",
    "orange"
    ]
    },
    {
    "model": "car_model_hoy_3",
    "basePrice": 32000,
    "colors":
    [
    "red",
    "white",
    "blue",
    "orange",
    "brown",
    "yellow",
    "purple"
    ]
    },
    {
    "model": "car_model_hoy_4",
    "basePrice": 34500,
    "colors":
    [
    "red",
    "white",
    "blue",
    "orange",
    "brown"
    ]
    },
    {
    "model": "car_model_hoy_5",
    "basePrice": 37000,
    "colors":
    [
    "red",
    "white",
    "blue",
    "orange",
    "brown"
    ]
    },
    {
    "model": "car_model_hoy_6",
    "basePrice": 54000,
    "colors":
    [
    "blue",
    "orange",
    "brown",
    "white"
    ]
    }
    ]
    }
    ],
    "trucks": [
    {
    "brand": "Toyo",
    "models": [
    {
    "model": "truck_model_1",
    "basePrice": 18000,
    "colors": [
    "red",
    "white",
    "blue"
    ]
    },
    {
    "model": "truck_model_2",
    "colors": [
    "red",
    "white",
    "blue",
    "gray",
    "sky"
    ]
    },
    {
    "model": "truck_model_3",
    "colors": [
    "red",
    "white",
    "blue",
    "green"
    ]
    }
    ]
    },
    {
    "brand": "Hoyo",
    "models": [
    {
    "model": "truck_model_hoy_1",
    "basePrice": 18000,
    "colors": [
    "red",
    "white",
    "blue"
    ]
    },
    {
    "model": "truck_model_hoy_2",
    "colors": [
    "red",
    "white",
    "blue",
    "yellow",
    "orange"
    ]
    },
    {
    "model": "truck_model_hoy_3",
    "basePrice": 32000,
    "colors": [
    "red",
    "white",
    "blue",
    "orange",
    "brown",
    "yellow",
    "purple"
    ]
    },
    {
    "model": "truck_model_hoy_4",
    "basePrice": 34500,
    "colors": [
    "red",
    "white",
    "blue",
    "orange",
    "brown"
    ]
    },
    {
    "model": "truck_model_hoy_5",
    "basePrice": 37000,
    "colors": [
    "red",
    "white",
    "blue",
    "orange",
    "brown"
    ]
    },
    {
    "model": "truck_model_hoy_6",
    "basePrice": 54000,
    "colors": [
    "blue",
    "orange",
    "brown",
    "white"
    ]
    }
    ]
    }
    ]
    }

  • @Mumbolian
    @Mumbolian Год назад +5

    I have been going through hours of content trying to work this out. You are the only person that made it make sense. Thank you very much!

    • @SoftwareNuggets
      @SoftwareNuggets  Год назад +1

      Hey @Mumbolian, appreciate you leaving this message. Wish you much success. -Scott

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

    Thank you for you quick response. My model is deeper and more complicated, but this gives me a starting point.

  • @Royur
    @Royur 2 года назад +2

    Thank you for this video. Been looking for hours on how to do this. Its really helpful.

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

      Hey Royur,
      Spending hours looking for the right video, I've been there too. I was very happy to read, this video was really helpful. I'd also like to thank you for leaving a message and subscribing to my channel. Thanks for the support.
      You might want to check out, "Use Visual Studios to generate C# class object to DESERIALIZE JSON", this video may speed up your development time when Deserializing JSON to C#.
      Thanks again.

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

    UGH, I was trying to use a List instead of an Array to deserialize thank you for clearing this up for me without having to watch an hour long video : )

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

      Hey Jason,
      Thanks for leaving a message and subscribing to my channel, I truly appreciate that. And very happy to hear,
      this video was able to shed some light on JSON arrays. I hope after watching this video, you were able to solve your issue.
      A couple of days ago, I released, "Use Visual Studios to generate C# class object to DESERIALIZE JSON", this video may speed up your development time when deserializing JSON to C#. It's a 4 minute video, but, I think you can get the jiff in 30 seconds or less.
      Thanks again, and have a great week.

  • @ahmadjerjees428
    @ahmadjerjees428 Месяц назад

    thanks for the video man !

    • @SoftwareNuggets
      @SoftwareNuggets  Месяц назад

      I hope this video was useful. thanks for leaving a comment @ahmadjerjees428

  • @yadav-r
    @yadav-r 2 года назад +1

    Thank you so much for explaining things so clearly, It helped me a lot. Thank you.

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

      Hey x3InThp!, glad to hear this video was useful. Thanks for leaving me a comment.

  • @Manuel_Roos
    @Manuel_Roos Год назад +1

    Awesome video. I have been searching a lot for an explanation this good. But how do I get out the values from eg. brand? Like I want to know what brands there are in the file?

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

      Hey @donmanuello, I think this video I created will help you
      ruclips.net/video/FLI_dzxdjDQ/видео.html
      After you watch this, and you still don’t have an answer, I’m going to continue helping you. We are going to understand this code, I promise.
      So, watch this video first, then reach back out.

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

      @@SoftwareNuggets Thank you so much. That helped a lot. I'm going to watch more of your videos. You're awesome

    • @SoftwareNuggets
      @SoftwareNuggets  Год назад +1

      Thanks for the feedback. After you finish reviewing, and you need some help, just give me a shout.

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

    Life saver!!!!!!!!!!!!!!!!!!!!!!!

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

    Thank you. Simple and easy to understand

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

      Hey Sri Varanasi,
      Thanks for taking the time to leave a comment, much appreciated. Glad to hear this video was useful.

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

    Perfect! Thanks for a great video.

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

      Hey Jim Grant,
      Saying "Perfect" is a huge compliment. Thank you very much.

  • @SharaPk
    @SharaPk 3 года назад

    great explanation, thanks for the video

    • @SoftwareNuggets
      @SoftwareNuggets  3 года назад

      Glad it was helpful! If you need any other assistance, please let me know. Thanks again.

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

    It would be great if you could also supply the source json and c# code. Thanks

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

      Hey abbot, I just pushed software as first comment, which is pinned.

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

    master, when we try to deserialize a complex json with a dinamic of names and colums do u have any exxample ?

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

      Did you see my video on Deserialize JSON to Dynamic C#?
      ruclips.net/video/nuifiGsgGA0/видео.html

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

      @@SoftwareNuggets thk u

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

    This helped a lot! One question I have is what if there was just one car array and 'cars' was not there in the response? How would the modeling be?

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

      Can you send me a sample json file to softwareNugget65@gmail.com and I will model for you.

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

      @@SoftwareNuggets wow thanks! Sent.

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

      Check your email

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

    Thank you. How can I make for and for-each models element only.

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

      Hey Faqruddin, thanks for viewing this video, as for your question, can you share a little more? You want to know how to use the [for, foreach] and loop the C# array elements?

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

      @@SoftwareNuggets yes. Assume Base class Name is ClaasBase and it has Salesid and Sales Name as normal objects and List is a list. So when I iterate the records first I want to refere base class i.e ClassBase and this class refere to List. How can I apply for or foreach so I can print SalesId, SalesName as normal values and SalesLines as list. Thank you.

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

      Can u send me the json sample data. SoftwareNugget65@gmail.com

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

      Hey Faqruddin, I have an example in one of my other videos, to "foreach" loop over the object
      Step-by-Step how to Deserialize JSON Two Dimensional Array using C# Newtonsoft Top video.
      ruclips.net/video/FLI_dzxdjDQ/видео.html
      if you move to around 7:50, it starts to show you how to loop over.
      Let me know if this helps.

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

      @@SoftwareNuggets Thank you for sharing. Please kindly check your email with subject Json For/Foreach Lists

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

    Good afternoon. great video! could you also provide the json file so I can simulate if it works on my end. thank you so much. I was looking at the "?" you use for null data, but is there a workaround for version .net framework 4.7.2? cause this has been introduced for later version.

    • @constantinekolouas8809
      @constantinekolouas8809 Год назад +1

      disregard. I saw that you posted in earlier comment. thanks!

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

      Can you please send me your program where nullable doesn't work. Include the project/solution, just zip up all that project, and send to me, if you'd like for me to look at it.
      Send to: softwareNugget65@gmail.com

  • @margaridalopes5810
    @margaridalopes5810 3 года назад

    Hey!
    first of all, great video!
    How do we access the properties that we want?

    • @SoftwareNuggets
      @SoftwareNuggets  3 года назад

      Hey Margarida Lopes, thanks for viewing the video and kind comment.
      Not sure exactly what you mean "access the properties" , but I will guess:
      Only have items in your C# class you want to extract from your JSON.
      here is an example:
      public class _model
      {
      public string model { get; set; }
      //public int? basePrice { get; set; }
      public string[] colors { get; set; }
      }
      noticed I commented out base_price.
      now, base_price is no longer in your c# class object, it was skipped from your json data.
      If I didn't answer your question, please try again with a different questions, with more details, and I am sure we will get it in the end.

    • @margaridalopes5810
      @margaridalopes5810 3 года назад +1

      @@SoftwareNuggets Hello once again!
      Yes, it answered my question!
      Thank you very much!
      You just saved me a lot of hours ahahah
      keep up the good work!

  • @JavierRodriguez-qf5zs
    @JavierRodriguez-qf5zs 2 года назад

    Hi, could it be possible to deserialize nested dictionaries instead of an array?

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

      Can you send me some sample json? Send to softwarenugget65@gmail.com

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

    I need some help reading a nested Json. Can we not use List?

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

      Hey avidity2002.
      Yes, you can use a list. This video will show you how.
      ruclips.net/video/lLMIvOd3lhk/видео.html
      please let me know if this solved your JSON problem.

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

      Advidity2002 - i just sent you two emails that will solve your issue.

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

    Sir, I have this code to extract json file. I have done same according to you. When I build project, it doesn't show any error but it doesn't show extracted values of feilds in the "data" field as you have shown through pointer. Please solve this.
    static void Main(string[] args)
    {

    string fileName = @"C:\prize.json";
    string jsonText = File.ReadAllText(fileName);
    var data = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText);
    Console.ReadLine();
    }
    public class Rootobject
    {
    public Prize[] prizes { get; set; }
    }
    public class Prize
    {
    public string year { get; set; }
    public string category { get; set; }
    public Laureate[] laureates { get; set; }
    public string overallMotivation { get; set; }
    }
    public class Laureate
    {
    public string id { get; set; }
    public string firstname { get; set; }
    public string surname { get; set; }
    public string motivation { get; set; }
    public string share { get; set; }
    }

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

      can you send me the json file. softwareNugget65@gmail.com

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

      {
      "year": "2022",
      "category": "programming",
      "laureates": [
      {
      "id": "1",
      "firstname": "one",
      "surname": "lastone",
      "motivation": "self",
      "share": "always"
      },
      {
      "id": "2",
      "firstname": "two",
      "surname": "lasttwo",
      "motivation": "self",
      "share": "always"
      }
      ],
      "overallmotivation": "highly motivated"
      }
      using System;
      namespace MyApp // Note: actual namespace depends on the project name.
      {
      internal class Program
      {
      static void Main(string[] args)
      {
      string fileName = @"C:\dev\prize.json";
      string jsonText = File.ReadAllText(fileName);
      var data = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText);
      Console.ReadLine();
      }
      public class Rootobject
      {
      public string year { get; set; }
      public string category { get; set; }
      public Laureate[] laureates { get; set; }
      public string overallMotivation { get; set; }
      }
      public class Laureate
      {
      public string id { get; set; }
      public string firstname { get; set; }
      public string surname { get; set; }
      public string motivation { get; set; }
      public string share { get; set; }
      }
      }
      }

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

    Hi can you help me ?

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

    Why does unity have to make JSON this difficult. It's json, why not just JSON.Parse and be done with it?

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

      I hear you. C# is a typed language, so, that's why. Did you see my video on Dynamic JSON. If your JSON isn't to complicated, that might work.
      ruclips.net/video/nuifiGsgGA0/видео.html
      Good luck mate.

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

      I just created you a new video, to generate C# with NO Coding. take a look if you are still interested.
      if you have time, please let me know if this video helped.
      Use Visual Studios to generate C# class object to DESERIALIZE JSON
      ruclips.net/video/5NOhai66okg/видео.html

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

      Hi can you help ?

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

      Hey @@kristelfernandez6717, how can I help you?