Developer technologies | .NET | .NET Multi-platform App UI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am loading list from api currently I have to change the code if I increase the questions in api , the number of questions are 5 , if I increase to 10 then code has to be changed , is there any way to make this dynamic so that I don't change the code even the questions are increased
async Task<List<InQuestion>> GetApiData()
{
string apiUrl = "https://sheet2api.com/v1/wlS0h0USxm9p/quiz";
List<InQuestion> Questions = new List<InQuestion>();
using (HttpClient client = new HttpClient())
{
try
{
// Make a GET request to the API
HttpResponseMessage response = await client.GetAsync(apiUrl);
// Check if the request was successful (status code 200)
if (response.IsSuccessStatusCode)
{
// Read the content of the response
string jsonString = await response.Content.ReadAsStringAsync();
// Deserialize the JSON string into an object
Questions = JsonConvert.DeserializeObject<List<InQuestion>>(jsonString);
// Now you can work with the 'apiData' object
Console.WriteLine("Received data:");
}
else
{
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
return Questions;
}
private async Task LoadQuestionsAsync()
{
List<InQuestion> res = await GetApiData();
Question q1 = new Question
{
QuestionTitle = res[0].QuestionTitle,
Options = new List<string>() { res[0].Opt1, res[0].Opt2, res[0].Opt3, res[0].Opt4 },
Answer = res[0].Answer };
Question q2 = new Question
{
QuestionTitle = res[1].QuestionTitle,
Options = new List<string>() { res[1].Opt1, res[1].Opt2, res[1].Opt3, res[1].Opt4 },
Answer = res[1].Answer
};
Question q3 = new Question
{
QuestionTitle = res[2].QuestionTitle,
Options = new List<string>() { res[2].Opt1, res[2].Opt2, res[2].Opt3, res[2].Opt4 },
Answer = res[2].Answer
};
Question q4 = new Question
{
QuestionTitle = res[3].QuestionTitle,
Options = new List<string>() { res[3].Opt1, res[3].Opt3, res[3].Opt3, res[3].Opt4 },
Answer = res[3].Answer
};
Question q5 = new Question
{
QuestionTitle = res[4].QuestionTitle,
Options = new List<string>() { res[4].Opt1, res[4].Opt3, res[4].Opt3, res[4].Opt4 },
Answer = res[4].Answer
};
Questions.AddRange(new List<Question> { q1, q2, q3, q4 ,q5});
}
public class InQuestion
{
public int No { get; set; }
public string QuestionTitle { get; set; }
public string Opt1 { get; set; }
public string Opt2 { get; set; }
public string Opt3 { get; set; }
public string Opt4 { get; set; }
public string Answer { get; set; }
}
public class Question
{
public string QuestionTitle { get; set; } = string.Empty;
public IEnumerable<string> Options { get; set; } = new List<string>();
public string Answer { get; set; } = string.Empty;
}
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Answer accepted by question author
Just map with select:
List<InQuestion> res = await GetApiData();
Questions.AddRange(res.Select(r => new Question
{
QuestionTitle = r.QuestionTitle,
Options = new List<string>() {r.Opt1, r.Opt2, r.Opt3, r.Opt4},
Answer = r.Answer
});