system.text.json not deserializing objects all request 0 i don't know to do (api)
Maybe someone know there my mistake ?
41 Replies
hm, why are you reading it as a string, saving it, then reading the saved file?
you already have the body content as a string, you could just use that instead of opening the file for reading
this method don't take this
so don't use
DeserializeAsync
use Deserialize
okey wait a second
also error
okay, can you show the actual json you get from the API?
since you save it to a file, should be easy to export
yes of corse
{"location":{"name":"Saint Petersburg","region":"Saint Petersburg City","country":"Russia","lat":59.894,"lon":30.264,"tz_id":"Europe/Moscow","localtime_epoch":1728232583,"localtime":"2024-10-06 19:36"},"current":{"last_updated_epoch":1728232200,"last_updated":"2024-10-06 19:30","temp_c":7.8,"temp_f":46.1,"is_day":0,"condition":{"text":"Clear","icon":"//cdn.weatherapi.com/weather/64x64/night/113.png","code":1000},"wind_mph":5.8,"wind_kph":9.4,"wind_degree":197,"wind_dir":"SSW","pressure_mb":1014.0,"pressure_in":29.94,"precip_mm":0.0,"precip_in":0.0,"humidity":79,"cloud":12,"feelslike_c":6.2,"feelslike_f":43.1,"windchill_c":6.2,"windchill_f":43.1,"heatindex_c":7.8,"heatindex_f":46.1,"dewpoint_c":4.4,"dewpoint_f":39.9,"vis_km":10.0,"vis_miles":6.0,"uv":1.0,"gust_mph":12.2,"gust_kph":19.7}}
is that the whole thing?
yes?
okay
so,,, yh
note how your structure doesnt match the json
at all
this is what the top level object looks like
it only has these two props
so when you try to deserialize that as your
current
class, it doesnt match and results in default values everywhereokey i make a difrent class and send you okey?
is all you need
just take that, deserialize to that and it should work just fine
excuse me please, I didn't quite understand what the problem is. Is it that the class and the structure of the response don't match or something else and I don't quite understand what to do with this line that you wrote...
yeah
JSON is structured, meaning its not just flat
the json you sent has a root object with two properties:
current
and location
your current
class seems to match the current
property on that root object, but we cant deserialize to that directly
the class you tell the serializer to use must match the structure of the json itself
so I made a record that matches the root objectOK, that's what I understood, and what kind of line did you send? this is somthing of class ?
yes
a record is a type of class
thx you are a best people in this world a go trying fix my code
удачи! (good luck)
Я удивлен что мне помогли в дискорде .А еще больше я удивлен что вы ответили мне на русском) (I'm surprised that they helped me in Discord. And I'm even more surprised that you answered me in Russian))
My russian is very basic, I mostly know dog commands and things you say to children 😄
but why would you be surprised that you got help? Thats what this discord is for.
Reddit and StackOverflow didn't help me
and it seems to me that the discord is smaller at least in terms of the number of people
reddit sucks, so thats why.
stack overflow is... hard to explain
they are trying to build a repository of unique questions
your question is probably not unique
that's why it's surprising to me
Well in that case I really shouldn't be surprised at anything
and one more question why did you say that you shouldn't use asynchronous Deserialize
?
its async because of streams, ie, reading from a file stream etc
because you already had the entire string completely in memory, we can use the sync
that is, because we have already used async to receive a response, we can not use it in deserialization?
to this I should organize the class for deserialization? if yes then how to access variables of nested class?
the other classes dont need to be nested
but they can be
more important is the properties
can you write an example of such a class, I don’t understand if it’s possible to nest them inside each other and will I then be able to call objects of the class?
yeah sure 1 sec
thx
this would work
or this,
and you would use it like...
c; thx i go try
EEEEEEE
I would like to know the name of this line, or rather what it is (array variable or something else that I can google and study)
are these properties?
yes
thx
all good all works