Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
these classes were generated by visual studio
C#
{
    public class Rootobject
    {
        public Bird[]? Albatros { get; set; }
        public Bird[]? Avocet { get; set; }
        public Bird[]? Bustard { get; set; }
        public Bird[]? Cassowary { get; set; }
        public Bird[]? Coot { get; set; }
        public Bird[]? Crake { get; set; }
        public Bird[]? Crane { get; set; }
        public Bird[]? Dotterel { get; set; }
        public Bird[]? Emoes { get; set; }
        public Bird[]? Fowl { get; set; }
        public Bird[]? Grebe { get; set; }
        public Bird[]? Gull { get; set; }
        public Bird[]? Hen { get; set; }
        public Bird[]? Heron { get; set; }
        public Bird[]? Ibis { get; set; }
        public Bird[]? Jacana { get; set; }
        public Bird[]? Mollymawk { get; set; }
        public Bird[]? Noddy { get; set; }
        public Bird[]? Oystercatcher { get; set; }
        public Bird[]? Penguin { get; set; }
        public Bird[]? Petrel { get; set; }
        public Bird[]? Pigeons { get; set; }
        public Bird[]? Plover { get; set; }
        public Bird[]? Prion { get; set; }
        public Bird[]? Quail { get; set; }
        public Bird[]? Rail { get; set; }
        public Bird[]? Sandpiper { get; set; }
        public Bird[]? Skua { get; set; }
        public Bird[]? Spoonbill { get; set; }
        public Bird[]? Stilt { get; set; }
        public Bird[]? Stork { get; set; }
        public Bird[]? StormPetrel { get; set; }
        public Bird[]? Tern { get; set; }
        public Bird[]? Ternlet { get; set; }
        public Bird[]? Turkey { get; set; }
        public Bird[]? Turnstone { get; set; }
        public Bird[]? Wanderer { get; set; }
        public Bird[]? Wonga { get; set; }
    }
    public class Bird
    {
        [JsonPropertyName("Ned")]
        public string? ned { get; set; }
        [JsonPropertyName("En")]
        public string? eng { get; set; }
        [JsonPropertyName("Fr")]
        public string? fra { get; set; }
        [JsonPropertyName("Lat")]
        public string? lat { get; set; }
        [JsonPropertyName("Bestand")]
        public string? imgfile { get; set; }
        [JsonPropertyName("Komt_voor_in")]
        public string? distribution { get; set; }
        [JsonPropertyName("zeldzaam")]
        public string? rarity { get; set; }
    }
    public enum Species // this one i made
    {
        Albatros,
        Avocet,
        Bustard,
        Cassowary,
        Coot,
        Crake,
        Crane,
        Dotterel,
        Emoes,
        Fowl,
        Grebe,
        Gull,
        Hen,
        Heron,
        Ibis,
        Jacana,
        Mollymawk,
        Noddy,
        Oystercatcher,
        Penguin,
        Petrel,
        Pigeons,
        Plover,
        Prion,
        Quail,
        Rail,
        Sandpiper,
        Skua,
        Spoonbill,
        Stilt,
        Stork,
        StormPetrel,
        Tern,
        Ternlet,
        Turkey,
        Turnstone,
        Wanderer,
        Wonga
    }
}

I want to input a specie as a Species and then select the birds belonging to the specie.
Also when I input the name or part of the name of a bird I want to indicate with a second input field whether the name is in eng, ned or lat or fr

The problem with the specie is that the RootObject is not enumerable, so I had to make a switch with all the species to get to the birds.
Same for the birds. I wonder it is possible to do something like this:
searchspecie = "Albatros"
Bird[] birds= birdsofaussie.searchspecie where birdsofaussie is the deserialized json file which was used to create the classes.

Thank you in advance for any suggestions

What I have tried:

This works
                string fileContents = LeesJson();
                Rootobject? birdsOfAustralia = JsonSerializer.Deserialize<Rootobject>(fileContents);
                Bird[] birds = birdsOfAustralia.Albatros;
                nofBirds = birds.Count();
                for (int j = 0; j < nofBirds; j++)
                {

                    if (birds[j].eng.IndexOf(SearchName, 0) >= 0)
                    {
                        gezochteVogels[gv] = birds[j];
                        gv++;
                    }
                }
                birds = birdsOfAustralia.Avocet;
                nofBirds = birds.Count();
                for (int j = 0; j < nofBirds; j++)
                {

                    if (birds[j].eng.IndexOf(SearchName, 0) >= 0)
                    {
                        gezochteVogels[gv] = birds[j];
                        gv++;
                    }
                }
...
Now I have to make exact the the same module but eng replaced by ned or fr or lat.
I want to avoid this and if possible use a foreach for the species
Posted
Updated 6-May-24 9:05am
v2

Do you have to keep your RootObject? If you change this around, you can use a Dictionary containing the species enumeration as the key, and the bird array as the values.
 
Share this answer
 
Comments
Eddy Sels 2021 7-May-24 5:58am    
not really, but I have not used dictionaries before (I'm not a professional programmer). I tried to make classes of my own, but they didn't work so I left it up to visual studio to create the classes from my json file. I wanted to experiment with json but was not aware json is not the best option to use as a database.
Andre Oosthuizen 8-May-24 14:37pm    
You can try this link to get started - Convert JSON to dictionary[^]
Eddy Sels 2021 8-May-24 15:00pm    
Thank you for the suggestion, but I have a working app in python. I'm porting it to C# and .NET Core.
In python I use this code:
while index_species < len(birds["Species"]):
while index_birds < len(birds["Species"][specie[index_species]]):
pattern=re.compile(dist_to_search,re.IGNORECASE)
if pattern.search(birds["Species"][specie[index_species]][index_birds]["Komt_voor_in"]) != None:
found_birds.append(birds["Species"][specie[index_species]][index_birds]["En"])
index_birds+=1
else:
index_birds+=1
index_species+=1
index_birds=0
So the deserialization of Json in python is more useful than the one in C#.
I also had to change my Json to be useful in C# (had to remove the species root)
It took me some reading to figure out dictionaries. Thanks for the suggestion, it put me on the right track.
C#
Dictionary<string, Bird[]> Soorten = new Dictionary<string, Bird[]>();
string fileContents = LeesJson();
Bird[]? birds = null;
Soorten = JsonSerializer.Deserialize<Dictionary<string, Bird[]>>(fileContents);
foreach (string s in Soorten.Keys)
{
    if(SelectedSpecie == s.ToString() && Soorten.TryGetValue(s, out Bird[]? Vogels))
    {
        birds = Vogels;
    }
}

I know I can omit the 'Vogels' and use 'birds' instead in the if statement.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900