✅ Filter Deserialized JSON data
My issue is that i want to filter a food list API's json data by the date and i am quite new to Newtonsoft.Json library and API in general. I can leave a snippet of the food list too .
32 Replies
Current Code:
1) dont use dynamic
you have a model use your model
what u would normally do is, you create an object that represents the json
then you deserialize to that object
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
this site helps you converting json sample data into a c# class
but since u did not post the whole JSON I cannot use it to show an example so I Will show a simple example of deserializing
so this is my class for the example
i dont really know where to use dynamic anyways 😛
that's good it would only dig your own grave with it
it gives output like this, but 22 of it
leowest
REPL Result: Success
Console Output
Compile: 493.649ms | Execution: 61.997ms | React with ❌ to remove this embed.
so this is an example of serializing using the class
and deserializing is just as easy
leowest
REPL Result: Success
Console Output
Compile: 509.795ms | Execution: 70.369ms | React with ❌ to remove this embed.
there we go
so as u can see in the last code we map the json to List<Menu>
and we can then access it normally as a natural object
so you could filter anything u want after u deserialize
u can even partially deserialize the data
is the items in the Menu class?
items is just the name of the variable I use to create a List<Menu> to serialize it and to deserialize it to
ah
my Menu class is
can you post the full JSON u are trying to deserialize to the site below? $paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
BlazeBin Basic - mcwzwebdreig
A tool for sharing your source code with the world!
so your class would look something like this
and in json [] means a list or array
so you would deserialize it to List<Menu>
oooooooh
didnt know that lol
is used to map json weird names to keep the c# convention naming
yea, i forgot to do that before lol
totally forgot
so yeah in a json viewer this is what ur data looks like
ye
my main issue is how can i filter by the
date
well you can use Linq for example
so after you deserialize the data
I wonder if u can use DateTime for the first date
let me try
DateTime.Now.ToString("dd-MM-yyyy");
worksyeah but if you can change the model from string to DateTime it is much easier to handle
oh
fair
yeah no it doesnt let me
you can do that if u want the menu for today
ty for your help
yw 😉 $close
Use the /close command to mark a forum thread as answered