I try to make a code to display organisms phylum, order, and kingdom, in lblOrganismIdentity
. Firstly, the user must input the species name in the tbNameSpecies
. And then click btProcess
to run the method on Organism class. And the name is used to make the API link. The API data is in this link for example. I dont know how to make the data to become an array, and then display it to lblOrganismIdentity
.
The exception error message is
System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary.'
The exception error occurs in this code
JsonObject kingdom = (JsonObject)gbifObj["kingdom"];
I'm using RestSharp and SimpleJson for the references. I've tried to search for the solutions, but I didn't get that.
This is the source code.
The GetOrganismList method in Organism.cs class
public static List<string> GetOrganismList(string link)
{
List<string> returnList = new List<string>();
var client = new RestClient(link);
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
JsonObject gbifObj = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(response.Content);
Console.WriteLine(gbifObj);
JsonObject kingdom = (JsonObject)gbifObj["kingdom"];
JsonObject phylum = (JsonObject)gbifObj["phylum"];
JsonObject order = (JsonObject)gbifObj["order"];
returnList.Add((string)kingdom["kingdom"]);
returnList.Add((string)phylum["phylum"]);
returnList.Add((string)order["order"]);
return returnList;
}
Form1.cs
public Form1()
{
InitializeComponent();
}
List<string> identityList = new List<string>();
private IEnumerable<string> identitylist;
private void btProcess_Click(object sender, EventArgs e)
{
DisplayInfo(identityList);
}
private void DisplayInfo(List<string> identityList)
{
string link = "http://api.gbif.org/v1/species/match?&name=" + tbNameSpecies.Text.ToString();
identityList = Organism.GetOrganismList(link);
lblOrganismIdentity.Text = "";
foreach (string item in identityList)
{
lblOrganismIdentity.Text += "- " + item + Environment.NewLine;
}
}
I expect the lblOrganismIdentity
can display data of the species from the user input.
For example:
kingdom: Animalia
phylum: Chordata
order: Carnivora