From f1e63c40f2fb739201a641849f2b48cfc2ae4128 Mon Sep 17 00:00:00 2001 From: Marcus Ferl Date: Fri, 1 Apr 2022 21:15:05 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=9EFunktions/Json=5FParse=5FExample.cs?= =?UTF-8?q?=E2=80=9C=20hinzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit init --- Funktions/Json_Parse_Example.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Funktions/Json_Parse_Example.cs diff --git a/Funktions/Json_Parse_Example.cs b/Funktions/Json_Parse_Example.cs new file mode 100644 index 0000000..f64fdde --- /dev/null +++ b/Funktions/Json_Parse_Example.cs @@ -0,0 +1,33 @@ +using Newtonsoft.Json; +using System.Net; + + +class JsonParser +{ + // Json file location + string json_url = "https://git.weifer.org/weifer/Pruefungskatalog/raw/branch/master/assets/questions_answers.json"; + + string get_questions = ""; + string get_answer = ""; + + // Get Data + dynamic get_jsonData(string url) + { + var webclient = new WebClient(); + string json = webclient.DownloadString(url); + dynamic dynJson = JsonConvert.DeserializeObject(json); + return dynJson; + + } + // Returns random String from a Random Json Object in JsonData + void get_question() + { + dynamic json_data = get_jsonData(json_url); + int count = json_data.Count; // Count Json Objects + int random_number = new Random().Next(0, count); // Random number between 0, and all Json Objects + + get_questions = json_data[random_number][$"{random_number}"]["question"]; + get_answer = json_data[random_number][$"{random_number}"]["answer"]; + + } +} \ No newline at end of file