C
C#

help

❔ How to parse a json response?

TTheHitchhiker11/11/2022
.
BBuddy11/11/2022
Deserialize into a class. You can use https://json2csharp.com/ that makes it easier to convert json into a C# class. Deserialize -> json (string) to object Serialize -> object to json (string)
TTheHitchhiker11/11/2022
thanks guys i got it right finally
TTheHitchhiker11/11/2022
AAngius11/11/2022
Why is there two people in the server, right now, trying to deserialize JSON to dynamic https://tenor.com/view/crying-emoji-gif-21922016 Another thing to add to my Solution1 talk I guess lol
TTheHitchhiker11/11/2022
lol i cant build such huge object, that what i really dont need. appretate your help though ❤️
AAngius11/11/2022
$jsongen
MMODiX11/11/2022
Use https://app.quicktype.io or https://json2csharp.com to generate classes from your JSON
SSwaggerLife11/12/2022
Try avoiding dynamic. Go with concrete objects.
public async Task<string> GetRandomJokeAsync()
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri($"https://api.humorapi.com/jokes/random?api-key={AppSettings.HumorApiKey}")
};

using var response = await _client.SendAsync(request);
response.EnsureSuccessStatusCode();

var data = JsonSerializer.Deserialize<Dictionary<string, object>>(await response.Content.ReadAsStringAsync()) ?? new Dictionary<string, object>();

return data.TryGetValue("joke", out object value) ? value.ToString() : string.Empty;
}
public async Task<string> GetRandomJokeAsync()
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri($"https://api.humorapi.com/jokes/random?api-key={AppSettings.HumorApiKey}")
};

using var response = await _client.SendAsync(request);
response.EnsureSuccessStatusCode();

var data = JsonSerializer.Deserialize<Dictionary<string, object>>(await response.Content.ReadAsStringAsync()) ?? new Dictionary<string, object>();

return data.TryGetValue("joke", out object value) ? value.ToString() : string.Empty;
}
AAngius11/12/2022
Dictionary<string, object> is only slightly less worse Also, your code seems to be using WebClient?
SSwaggerLife11/12/2022
You are total right, as I said a concrete object is much better. But I got lazy there. I'm not sure what you mean by WebClient? No it's using HttpClient! if that's what you mean.
AAngius11/12/2022
Ah, my mistake then Seemed too complex for a httpclient, since you can just use await client.GetAsJsonAsync<MyClass>(url);
SSwaggerLife11/12/2022
Oh maybe I overcomplicated it. I wasn't aware you could do await client.GetAsJsonAsync<MyClass>(url); yeah that for sure simplifies it. Good to know.
AAccord11/22/2022
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ Task.WhenAll for Tests, and other async questionsIn xUnit, there is no support for testing async or in parallel within the same collection, where a cEntity Framework Core DB issues [Answered]Introducing FOREIGN KEY constraint 'FK_Sections_Teachers_TeacherID' on table 'Sections' may cause cy❔ Reverse proxy + controllers ordering issuesPlease help https://github.com/microsoft/reverse-proxy/discussions/792#discussioncomment-4119355❔ where generic is itypea or itypebIs there syntax to check weather a generic attribute is either of two interface types? Pseudo: Wher❔ A question about struct - interface implementationStructs that implement interfaces arent allocated on the heap normally, rather just if being cast to❔ Calculate AreaHi guys! I need some help about a task I've been stuck for a while. I follow a programing course and❔ Blazor WASM PWA clear all cache and update applicationMy app does not update automatically on new versions (I use WASM). Since it's PWA the `no-cache` hea❔ Render HTML classes based on properties (Blazor)So we managed to setup a Linq expression to get all matching tags from two lists and all. Which is a❔ Does WebClient have the same socket exhaustion problem like HttpClient?title❔ SerilogLet's say i have a following log: `Logger.Information("You have earn {amount}{currency}", 10, "$")` ❔ proceduresHow do I declare a procedure or a function, I'm in a lesson but they only showed me how to do it in ✅ Consuming REST API in MAUII'm pretty new to MAUI and I want to make an app that gets some data from a REST API and presents itHelp refactor please? I might have missed something big here [Answered]is there maybe a better way of writing this? ```cs foreach (Experience experience in User.Profile.Ex❔ Models data is not showing in view with help of ViewDataI have created model and passed its data with ViewData like this- Controller Code--> IEnumerable<Se✅ Software architecture for front-end translationsI've an .NET 7 Blazor WebAssembly Hosted web application meant to be deployed in a industrial enviro❔ Help with designing a functional container objectHi, I could use some input for a functional container/result object I'm working on. I want it (the s❔ Enumerable.Current is not null in debug mode but assigns null to variableI know I'm not exactly following the best practices with the reader but this code should work none tJSON Array to build a complex object isn't letting me create multiple different properties inside itHi! I'm having an issue serializing a JSON object to create a Microsoft Band WebTile. Here is my cod❔ Proper mocking for Unit Testing (NUnit)```csharp public class TargetServiceSetEndDateAsTwoMonthsOut : PreProcessorAction<CreateTargetServic❔ problem datetime```c EndSeason = 25/11/2022 05:45, ``` ```c public DateTime EndSeason { get; set; } ```