Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Get random object form JSON File

C# Get random object form JSON File

Scheduled Pinned Locked Moved C#
csharpjsontutoriallinqgraphics
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pavlex4
    wrote on last edited by
    #1

    How to get random strings from JSON Object? For example when first time program is run on when I click on button a1 button1.Text should be "SUVO",when click on button b3 button8.Text should be "TRIJUMFALNA KAPIJA" and so on... [ { "a1": "SUVO", "a2": "SLATKO", "a3": "BELO", "a4": "ISTINA", "a5": [ "vino" ], "b1": "LOVOR", "b2": "POSTOLJE", "b3": "TRIJUMFALNA KAPIJA", "b4": "DAN", "b5": [ "pobeda", "pobede", "pobjeda" ], "c1": "UEFA", "c2": "TAKMIČENJE", "c3": "ELIMINACIJA", "c4": "ŠAMPIONA", "c5": [ "kup" ], "d1": "ISUS", "d2": "KRV", "d3": "VITEZOVI", "d4": "OKRUGLI STO", "d5": [ "sveti gral", "svijeti gral", "svjeti gral", "gral" ], "rr": [ "pehar", "pjehar" ] } ] When clicked button that is called "new game" buttons should have this text: And now when clicked on button a1 button1.Text now should be "hudini",when click on button b3 button8.Text should now be "zid" and so on... { "a1": "hudini", "a2": "trik", "a3": "plašt", "a4": "predstava", "a5": [ "mađioničar", "madjionicar", "čarobnjak", "carobnjak", "madjioničar" ], "b1": "svila", "b2": "bicikl", "b3": "zid", "b4": "država", "b5": [ "kina" ], "c1": "kornet", "c2": "frikom", "c3": "delta", "c4": "kugla", "c5": [ "sladoled" ], "d1": "školjka", "d2": "aparat", "d3": "sluh", "d4": "bubanj", "d5": [ "uši", "uvo", "usi", "uho" ], "rr": [ "štapić", "stapic", "štap", "stap" ] }, Source code of program:

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

    namespace Demo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            List data;
            private void Form1\_Load(object sender, EventArgs e)
            {
                data = GetJsonFromFile(@"MyData.json");
            }
            private List GetJsonFromFile(string path)
            {
                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(path))))
                {  
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List));
                    List data = (List)serializer.ReadObject(stream);
                    return data;
    
    L 1 Reply Last reply
    0
    • P Pavlex4

      How to get random strings from JSON Object? For example when first time program is run on when I click on button a1 button1.Text should be "SUVO",when click on button b3 button8.Text should be "TRIJUMFALNA KAPIJA" and so on... [ { "a1": "SUVO", "a2": "SLATKO", "a3": "BELO", "a4": "ISTINA", "a5": [ "vino" ], "b1": "LOVOR", "b2": "POSTOLJE", "b3": "TRIJUMFALNA KAPIJA", "b4": "DAN", "b5": [ "pobeda", "pobede", "pobjeda" ], "c1": "UEFA", "c2": "TAKMIČENJE", "c3": "ELIMINACIJA", "c4": "ŠAMPIONA", "c5": [ "kup" ], "d1": "ISUS", "d2": "KRV", "d3": "VITEZOVI", "d4": "OKRUGLI STO", "d5": [ "sveti gral", "svijeti gral", "svjeti gral", "gral" ], "rr": [ "pehar", "pjehar" ] } ] When clicked button that is called "new game" buttons should have this text: And now when clicked on button a1 button1.Text now should be "hudini",when click on button b3 button8.Text should now be "zid" and so on... { "a1": "hudini", "a2": "trik", "a3": "plašt", "a4": "predstava", "a5": [ "mađioničar", "madjionicar", "čarobnjak", "carobnjak", "madjioničar" ], "b1": "svila", "b2": "bicikl", "b3": "zid", "b4": "država", "b5": [ "kina" ], "c1": "kornet", "c2": "frikom", "c3": "delta", "c4": "kugla", "c5": [ "sladoled" ], "d1": "školjka", "d2": "aparat", "d3": "sluh", "d4": "bubanj", "d5": [ "uši", "uvo", "usi", "uho" ], "rr": [ "štapić", "stapic", "štap", "stap" ] }, Source code of program:

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

      namespace Demo
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
              List data;
              private void Form1\_Load(object sender, EventArgs e)
              {
                  data = GetJsonFromFile(@"MyData.json");
              }
              private List GetJsonFromFile(string path)
              {
                  using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(path))))
                  {  
                      DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List));
                      List data = (List)serializer.ReadObject(stream);
                      return data;
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Pavlex4 wrote:

      How to get random strings

      You use the Random class to generate a random number and pull that index from whatever string collection you have.

      Pavlex4 wrote:

      btn.Text.ToString()

      Every time I see this I wonder about it and every time I ask, I don't get an answer. Maybe you can tell me what benefit is it that you think you get from converting a string to a string?

      Speed of sound - 1100 ft/sec Speed of light - 186,000 mi/sec Speed of stupid - instantaneous.

      L OriginalGriffO 2 Replies Last reply
      0
      • L Lost User

        Pavlex4 wrote:

        How to get random strings

        You use the Random class to generate a random number and pull that index from whatever string collection you have.

        Pavlex4 wrote:

        btn.Text.ToString()

        Every time I see this I wonder about it and every time I ask, I don't get an answer. Maybe you can tell me what benefit is it that you think you get from converting a string to a string?

        Speed of sound - 1100 ft/sec Speed of light - 186,000 mi/sec Speed of stupid - instantaneous.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It's like the "Learner Driver" notice affixed to the rear of a vehicle. Until they actually learn to remove it, we can only assume.

        L 1 Reply Last reply
        0
        • L Lost User

          Pavlex4 wrote:

          How to get random strings

          You use the Random class to generate a random number and pull that index from whatever string collection you have.

          Pavlex4 wrote:

          btn.Text.ToString()

          Every time I see this I wonder about it and every time I ask, I don't get an answer. Maybe you can tell me what benefit is it that you think you get from converting a string to a string?

          Speed of sound - 1100 ft/sec Speed of light - 186,000 mi/sec Speed of stupid - instantaneous.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Check out his other questions and you'll find he doesn't know, and doesn't appear to be trying to learn anything. At a guess, he has conned his way into a job he has no idea how to do, and is trying to use Google and us to keep him getting paid. A help vampire, in fact.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          L 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Check out his other questions and you'll find he doesn't know, and doesn't appear to be trying to learn anything. At a guess, he has conned his way into a job he has no idea how to do, and is trying to use Google and us to keep him getting paid. A help vampire, in fact.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            OriginalGriff wrote:

            A help vampire

            Indeed. One of the reasons I asked was to see if he actually had a rational explanation. I expect not.

            Speed of sound - 1100 ft/sec Speed of light - 186,000 mi/sec Speed of stupid - instantaneous.

            1 Reply Last reply
            0
            • L Lost User

              It's like the "Learner Driver" notice affixed to the rear of a vehicle. Until they actually learn to remove it, we can only assume.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Gerry Schmitz wrote:

              "Learner Driver"

              Yes, I believe you are correct sir. It amazes me that how some people call themselves developers nowadays that don't have any idea how computers work, what a file is, what a path is, what a directory is, etc.

              Speed of sound - 1100 ft/sec Speed of light - 186,000 mi/sec Speed of stupid - instantaneous.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups