„Funktions/Json_Parse_Example.cs“ hinzufügen

init
This commit is contained in:
Marcus Ferl 2022-04-01 21:15:05 +02:00
parent 73dae5cbc6
commit f1e63c40f2

View File

@ -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"];
}
}