C# Get random object form JSON File
-
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;
-
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;
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.
-
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.
-
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.
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...
-
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...
-
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.
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.