Decabytes
Decabytes
CC#
Created by Decabytes on 11/19/2023 in #help
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?
10 replies
CC#
Created by Decabytes on 9/2/2023 in #help
❔ Polyglot notebooks not working on Manjaro Linux
5 replies
CC#
Created by Decabytes on 2/17/2023 in #help
❔ Class method construction using lambda?
I was looking at some Avalonia code and I noticed this code internal class Database { public IEnumerable<TodoItem> Get public IEnumerable<TodoItem> GetItems() => new[] { new TodoItem{Description = "Walk the dog" }, new TodoItem{Description = "Buy some milk"}, new TodoItem{Description = "Learn Avalonia", isChecked = true } }; I'm confused about the => new[] part. If the method had just been public IEnumerable<TodoItem> GetItems() { new TodoItem{Description = "Walk the dog" }, new TodoItem{Description = "Buy some milk"}, new TodoItem{Description = "Learn Avalonia", isChecked = true } }; I wouldn't have batted an eye. Does => new[] just mean collect the results in a curly brace into an array? What is this feature called?
8 replies
CC#
Created by Decabytes on 2/2/2023 in #help
❔ Trouble Creating PowerShell Commandlet
6 replies