C
C#8mo ago
alperenk._

✅ Deserializing nested array json file.

I am trying to get the json file data and store it in the class. But Storing in the class part doesnt work. Weatherdata.current is null
No description
No description
12 Replies
Pobiega
Pobiega8mo ago
can you paste some example json here? I can see a few things that are likely wrong one: its recommended to name your properties with C# standard names, and use [JsonPropertyName] to support snake-case names. For properties that just need to be Current-> current, thats easily fixed by just telling the serializer to use the web defaults
var options = new JsonSerializerOptions(JsonDefaults.Web);
var response = JsonSerializer.Deserialize<T>(json, options);
var options = new JsonSerializerOptions(JsonDefaults.Web);
var response = JsonSerializer.Deserialize<T>(json, options);
I'm fairly sure your problem is because your props in the root object are using pascal case, but you dont have web standards configured for your serializer you should also pretty much never be using a HttpClient
alperenk._
alperenk._OP8mo ago
this is the whole json
alperenk._
alperenk._OP8mo ago
thanks for the response. so should i change my props according to the file ?
Pobiega
Pobiega8mo ago
no, you should tell your serializer to use web defaults.
alperenk._
alperenk._OP8mo ago
Is it better in my case if i use restsharp
Pobiega
Pobiega8mo ago
no. restsharp is garbage.
alperenk._
alperenk._OP8mo ago
😄 alright
Pobiega
Pobiega8mo ago
HttpClient is all you need with GetFromJsonAsync var response = await client.GetFromJsonAsync<ApiResponse>("..."); will do 99% of the job for youi it will use web defaults, and you would need to change pretty much nothing else.. I personally would fix my response models to use proper C# naming and use attributes to "correct" the snake case names thou
alperenk._
alperenk._OP8mo ago
alright thank you so much trying to make it and changing the prop names rn
Pobiega
Pobiega8mo ago
var json = "{\"current\":{\"time\":\"2021-10-06T12:00:00Z\",\"temperature_2m\":20.0,\"wind_speed_10m\":5.0}}";
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
var apiResponse = JsonSerializer.Deserialize<ApiResponse>(json, options);

// or with a http client...

apiResponse = await httpClient.GetFromJsonAsync<ApiResponse>("https://...");

Console.WriteLine($"Time: {apiResponse.Current.Time}");
Console.WriteLine($"Temperature: {apiResponse.Current.Temperature2M}");
Console.WriteLine($"Wind Speed: {apiResponse.Current.WindSpeed10M}");

public record ApiResponse(CurrentData Current);

public record CurrentData(
string Time,
[property: JsonPropertyName("temperature_2m")]
double Temperature2M,
[property: JsonPropertyName("wind_speed_10m")]
double WindSpeed10M
);
var json = "{\"current\":{\"time\":\"2021-10-06T12:00:00Z\",\"temperature_2m\":20.0,\"wind_speed_10m\":5.0}}";
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
var apiResponse = JsonSerializer.Deserialize<ApiResponse>(json, options);

// or with a http client...

apiResponse = await httpClient.GetFromJsonAsync<ApiResponse>("https://...");

Console.WriteLine($"Time: {apiResponse.Current.Time}");
Console.WriteLine($"Temperature: {apiResponse.Current.Temperature2M}");
Console.WriteLine($"Wind Speed: {apiResponse.Current.WindSpeed10M}");

public record ApiResponse(CurrentData Current);

public record CurrentData(
string Time,
[property: JsonPropertyName("temperature_2m")]
double Temperature2M,
[property: JsonPropertyName("wind_speed_10m")]
double WindSpeed10M
);
you should be able to implement the hourly stuff yourself 🙂 but see how easy it is when using the proper method on the client, just the one row
alperenk._
alperenk._OP8mo ago
I really appreciate it man 😄 Just made the hourly 😃 thank you
MODiX
MODiX8mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server