C
C#2mo ago
LuxiPew

system.text.json not deserializing objects all request 0 i don't know to do (api)

Maybe someone know there my mistake ?
41 Replies
Pobiega
Pobiega2mo ago
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
LuxiPew
LuxiPewOP2mo ago
this method don't take this
No description
Pobiega
Pobiega2mo ago
so don't use DeserializeAsync use Deserialize
LuxiPew
LuxiPewOP2mo ago
okey wait a second
LuxiPew
LuxiPewOP2mo ago
No description
LuxiPew
LuxiPewOP2mo ago
also error
LuxiPew
LuxiPewOP2mo ago
No description
Pobiega
Pobiega2mo ago
okay, can you show the actual json you get from the API? since you save it to a file, should be easy to export
LuxiPew
LuxiPewOP2mo ago
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}}
Pobiega
Pobiega2mo ago
is that the whole thing?
LuxiPew
LuxiPewOP2mo ago
yes?
Pobiega
Pobiega2mo ago
okay so,,, yh note how your structure doesnt match the json at all
Pobiega
Pobiega2mo ago
No description
Pobiega
Pobiega2mo ago
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 everywhere
LuxiPew
LuxiPewOP2mo ago
okey i make a difrent class and send you okey?
Pobiega
Pobiega2mo ago
public record WeatherResponse(current current);
public record WeatherResponse(current current);
is all you need just take that, deserialize to that and it should work just fine
LuxiPew
LuxiPewOP2mo ago
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...
Pobiega
Pobiega2mo ago
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 object
LuxiPew
LuxiPewOP2mo ago
OK, that's what I understood, and what kind of line did you send? this is somthing of class ?
No description
Pobiega
Pobiega2mo ago
yes a record is a type of class
LuxiPew
LuxiPewOP2mo ago
thx you are a best people in this world a go trying fix my code
Pobiega
Pobiega2mo ago
удачи! (good luck)
LuxiPew
LuxiPewOP2mo ago
Я удивлен что мне помогли в дискорде .А еще больше я удивлен что вы ответили мне на русском) (I'm surprised that they helped me in Discord. And I'm even more surprised that you answered me in Russian))
Pobiega
Pobiega2mo ago
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.
LuxiPew
LuxiPewOP2mo ago
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
Pobiega
Pobiega2mo ago
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
LuxiPew
LuxiPewOP2mo ago
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 ?
Pobiega
Pobiega2mo ago
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
LuxiPew
LuxiPewOP2mo ago
that is, because we have already used async to receive a response, we can not use it in deserialization?
LuxiPew
LuxiPewOP2mo ago
to this I should organize the class for deserialization? if yes then how to access variables of nested class?
No description
No description
No description
Pobiega
Pobiega2mo ago
the other classes dont need to be nested but they can be more important is the properties
LuxiPew
LuxiPewOP2mo ago
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?
Pobiega
Pobiega2mo ago
yeah sure 1 sec
LuxiPew
LuxiPewOP2mo ago
thx
Pobiega
Pobiega2mo ago
public class RootObject
{
public Location Location { get; set; }
public Current Current { get; set; }
}

public class Current
{
// ...
}

public class Location
{
// ...
}
public class RootObject
{
public Location Location { get; set; }
public Current Current { get; set; }
}

public class Current
{
// ...
}

public class Location
{
// ...
}
this would work or this,
public class RootObject
{
public Location location { get; set; }
public Current current { get; set; }

public class Current
{
// ...
}

public class Location
{
// ...
}
}
public class RootObject
{
public Location location { get; set; }
public Current current { get; set; }

public class Current
{
// ...
}

public class Location
{
// ...
}
}
and you would use it like...
var root = JsonSerializer.Deserialize<RootObject>(responseBody);
Console.WriteLine(root.current.temperature);
var root = JsonSerializer.Deserialize<RootObject>(responseBody);
Console.WriteLine(root.current.temperature);
LuxiPew
LuxiPewOP2mo ago
c; thx i go try
LuxiPew
LuxiPewOP2mo ago
EEEEEEE
No description
LuxiPew
LuxiPewOP2mo ago
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)
No description
LuxiPew
LuxiPewOP2mo ago
are these properties?
Pobiega
Pobiega2mo ago
yes
LuxiPew
LuxiPewOP2mo ago
thx all good all works
Want results from more Discord servers?
Add your server