Csharp_Snippets/Funktions/Json_Parse_Example.cs

41 lines
1.1 KiB
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)
{
string json = "";
var webclient = new WebClient();
try
{
json = webclient.DownloadString(url);
}
catch(WebException e)
{
throw e;
}
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"];
}
}