FilteringJSONData - lucyberryhub/WPF.Tutorial GitHub Wiki

πŸ’ Lesson 1: Finding the Juiciest Berries! (Filtering JSON Data)

Imagine you have a big basket full of berries (JSON data), and you only want the strawberries (specific group of data). We need a way to pick out just the berries we want! 🌱

πŸ“ How Does It Work?

Let’s say we have a list of berries:

[
    { "Name": "Strawberry", "Color": "Red", "Group": "Sweet" },
    { "Name": "Blueberry", "Color": "Blue", "Group": "Sour" },
    { "Name": "Raspberry", "Color": "Red", "Group": "Sweet" },
    { "Name": "Blackberry", "Color": "Black", "Group": "Sour" }
]

Now, let’s say we only want the sweet berries. How do we do that? We filter them out! πŸ“

πŸ“ How Do We Pick Only the Sweet Berries?

Using magic berry picking powers (a filter function), we can only select the berries that belong to the Sweet group:

var sweetBerries = allBerries.Where(berry => berry.Group == "Sweet").ToList();

This code goes through all the berries and picks out only the ones in the "Sweet" group. Now, sweetBerries will contain:

[
    { "Name": "Strawberry", "Color": "Red", "Group": "Sweet" },
    { "Name": "Raspberry", "Color": "Red", "Group": "Sweet" }
]

✨ Wow! We have picked only the sweet berries! ✨

πŸ›  Try It Yourself:

  • Change "Sweet" to "Sour" and see what happens!
  • What if you want only red berries? (Hint: Filter by "Color" == "Red")

Now you know how to pick only the juiciest berries from a big basket! πŸŽ‰