Csharp_Snippets/Funktions/Json_Parse_Example.cs

33 lines
998 B
C#
Raw Normal View History

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