❔ How to parse a json response?

.
15 Replies
Buddy
Buddy3y ago
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)
TheHitchhiker
TheHitchhikerOP3y ago
thanks guys i got it right finally
TheHitchhiker
TheHitchhikerOP3y ago
Angius
Angius3y ago
Why is there two people in the server, right now, trying to deserialize JSON to dynamic
Angius
Angius3y ago
Another thing to add to my Solution1 talk I guess lol
TheHitchhiker
TheHitchhikerOP3y ago
lol i cant build such huge object, that what i really dont need. appretate your help though ❤️
Angius
Angius3y ago
$jsongen
MODiX
MODiX3y ago
Use https://app.quicktype.io or https://json2csharp.com to generate classes from your JSON
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.
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
V0FBU1VM
V0FBU1VM3y ago
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;
}
Angius
Angius3y ago
Dictionary<string, object> is only slightly less worse Also, your code seems to be using WebClient?
V0FBU1VM
V0FBU1VM3y ago
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.
Angius
Angius3y ago
Ah, my mistake then Seemed too complex for a httpclient, since you can just use await client.GetAsJsonAsync<MyClass>(url);
V0FBU1VM
V0FBU1VM3y ago
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.
Accord
Accord3y ago
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.
Want results from more Discord servers?
Add your server