C
C#12mo ago
Decabytes

CatAPI Json deserialization issue.

I'm having trouble getting my code to deserialize my json correctly. When I run my Program.cs I get
API Response: [{"id":"530","url":"https://cdn2.thecatapi.com/images/530.jpg","width":448,"height":538}]
Deserialized Cat URL:
value=HereKitty.RandomCat
url=
API Response: [{"id":"530","url":"https://cdn2.thecatapi.com/images/530.jpg","width":448,"height":538}]
Deserialized Cat URL:
value=HereKitty.RandomCat
url=
But I'm not able to access the fields in the json. My Program.cs
var filePath = "/home/deca/auth/cat_api.json";
using var doc = JsonDocument.Parse(File.ReadAllText(filePath));
var apiKey = doc.RootElement.GetProperty("api_key").GetString();
var kitty = new HereKitty(apiKey);

var response = await kitty.GetRandomCatAsync();
Console.WriteLine($"value={response}");
Console.WriteLine($"url={response.Url}");
var filePath = "/home/deca/auth/cat_api.json";
using var doc = JsonDocument.Parse(File.ReadAllText(filePath));
var apiKey = doc.RootElement.GetProperty("api_key").GetString();
var kitty = new HereKitty(apiKey);

var response = await kitty.GetRandomCatAsync();
Console.WriteLine($"value={response}");
Console.WriteLine($"url={response.Url}");
my Json class
public class RandomCat
{
public string Id { get; set; }
public string Url { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
public class RandomCat
{
public string Id { get; set; }
public string Url { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
the response
{"id":"530","url":"https://cdn2.thecatapi.com/images/530.jpg","width":448,"height":538}
{"id":"530","url":"https://cdn2.thecatapi.com/images/530.jpg","width":448,"height":538}
my function
public async Task<RandomCat> GetRandomCatAsync()
{
var response = await _httpClient.GetAsync("images/search");
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine($"API Response: {responseBody}");
using var jData = JsonDocument.Parse(responseBody);
var catData = jData.RootElement[0]; // Access the first element of the JSON array

RandomCat cat = JsonSerializer.Deserialize<RandomCat>(catData.GetRawText()); // Deserialize the first element
Console.WriteLine($"Deserialized Cat URL: {cat?.Url}");
return cat;
}
public async Task<RandomCat> GetRandomCatAsync()
{
var response = await _httpClient.GetAsync("images/search");
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine($"API Response: {responseBody}");
using var jData = JsonDocument.Parse(responseBody);
var catData = jData.RootElement[0]; // Access the first element of the JSON array

RandomCat cat = JsonSerializer.Deserialize<RandomCat>(catData.GetRawText()); // Deserialize the first element
Console.WriteLine($"Deserialized Cat URL: {cat?.Url}");
return cat;
}
Does anyone know what I'm doing wrong?
7 Replies
Angius
Angius12mo ago
The [] means the response is an array, so could be that Also, why fiddle around with non-type-safe JsonDocument and what not instead of just... parsing the response to JSON?
Decabytes
DecabytesOP12mo ago
Ahh Thank you! GetFromJsonAsync works so much better! The other way I was doing it was
var response = await kitty.GetRandomCatAsync();
using var jData = JsonDocument.Parse(response);
var jDoc = jData.RootElement;
var catData = jDoc[0];
var url = catData.GetProperty("url").GetString();
var response = await kitty.GetRandomCatAsync();
using var jData = JsonDocument.Parse(response);
var jDoc = jData.RootElement;
var catData = jDoc[0];
var url = catData.GetProperty("url").GetString();
which was pain.
PixxelKick
PixxelKick12mo ago
yeah its returning an array of RandomCat, not just 1
No description
PixxelKick
PixxelKick12mo ago
it just happens to only be 1 item long So you wanna do JsonSerializer.Deserialize<RandomCat[]>
dan in a can
dan in a can12mo ago
glad it works!
Mayor McCheese
Mayor McCheese12mo ago
Even just one cat has 9 lives
Want results from more Discord servers?
Add your server