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! π