Xraxis
Xraxis
CC#
Created by Xraxis on 3/20/2024 in #help
Question about enabling filtering of items
Hello, I was curious if anyone had any idea on how to assign data to visual objects in .Net Maui? I have a list of trading cards that I am putting the image of into a grid with a couple other elements and then want to be able to set the visibility of the image and other elements based on a set of filters created using buttons or checkboxes. The main issue I am having is I can't figure out how to set the the properties of the card to be filtered. I don't know how good of an explaination of what I am asking that was so if anyone wants more info about what I am looking for just ask.
1 replies
CC#
Created by Xraxis on 3/12/2024 in #help
unable to parse Json data pulled from API as a list of strings
I am trying to take some data from an API called Scryfall API and assign it to a List variable. I am very new to this and have as of yet been unsuccessful in saving the URLs to the images in the API as a list of strings that I am going to use later. If anyone has any ideas I really could use the help. The main line of code I am using is:
List<string> imageUrls = jsonResponse["data"].Select(item => item["png"].ToString()).ToList();
List<string> imageUrls = jsonResponse["data"].Select(item => item["png"].ToString()).ToList();
Here is the rest of my retrieval code:
using Newtonsoft.Json.Linq;

namespace MBT.API
{
public class APIHelper
{

// Limit the number of requests in a time period
private readonly int MaxRequestsPerSecond = 15;
private readonly TimeSpan RequestInterval = TimeSpan.FromSeconds(1);
private DateTime lastRequestTime = DateTime.MinValue;
private int requestCount = 0;

private static HttpClient apiClient = new()
{

BaseAddress = new Uri("https://api.scryfall.com/cards/search?q=type%3Aland+-t%3Acreature+-t%3Aartifact+-t%3Aenchantment+-t%3Aplaneswalker+-t%3Asorcery+-t%3Ainstant"),

};

public async Task<List<string>> GetLandImages()
{

var url = "https://api.scryfall.com/cards/search?q=type%3Aland+-t%3Acreature+-t%3Aartifact+-t%3Aenchantment+-t%3Aplaneswalker+-t%3Asorcery+-t%3Ainstant";
var response = await apiClient.GetAsync(url);
bool requestCountStatus = CanMakeRequest();

using (apiClient = new HttpClient(new HttpClientHandler
{

SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13

}))
{

if (response.IsSuccessStatusCode && requestCountStatus)
{

string responseBody = await response.Content.ReadAsStringAsync();
JObject jsonResponse = JObject.Parse(responseBody);

if(jsonResponse["data"] != null && jsonResponse["data"].HasValues)
{

List<string> imageUrls = jsonResponse["data"].Select(item => item["png"].ToString()).ToList(); //Broken. Need to fix.
return imageUrls;

}

return new List<string>();

}

return new List<string>();

}
}

bool CanMakeRequest()
{
DateTime now = DateTime.Now;

if (now - lastRequestTime > RequestInterval)
{
lastRequestTime = now;
requestCount = 0;
}

if (requestCount < MaxRequestsPerSecond)
{
requestCount++;
return true;
}

return false;
}
}
}
using Newtonsoft.Json.Linq;

namespace MBT.API
{
public class APIHelper
{

// Limit the number of requests in a time period
private readonly int MaxRequestsPerSecond = 15;
private readonly TimeSpan RequestInterval = TimeSpan.FromSeconds(1);
private DateTime lastRequestTime = DateTime.MinValue;
private int requestCount = 0;

private static HttpClient apiClient = new()
{

BaseAddress = new Uri("https://api.scryfall.com/cards/search?q=type%3Aland+-t%3Acreature+-t%3Aartifact+-t%3Aenchantment+-t%3Aplaneswalker+-t%3Asorcery+-t%3Ainstant"),

};

public async Task<List<string>> GetLandImages()
{

var url = "https://api.scryfall.com/cards/search?q=type%3Aland+-t%3Acreature+-t%3Aartifact+-t%3Aenchantment+-t%3Aplaneswalker+-t%3Asorcery+-t%3Ainstant";
var response = await apiClient.GetAsync(url);
bool requestCountStatus = CanMakeRequest();

using (apiClient = new HttpClient(new HttpClientHandler
{

SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13

}))
{

if (response.IsSuccessStatusCode && requestCountStatus)
{

string responseBody = await response.Content.ReadAsStringAsync();
JObject jsonResponse = JObject.Parse(responseBody);

if(jsonResponse["data"] != null && jsonResponse["data"].HasValues)
{

List<string> imageUrls = jsonResponse["data"].Select(item => item["png"].ToString()).ToList(); //Broken. Need to fix.
return imageUrls;

}

return new List<string>();

}

return new List<string>();

}
}

bool CanMakeRequest()
{
DateTime now = DateTime.Now;

if (now - lastRequestTime > RequestInterval)
{
lastRequestTime = now;
requestCount = 0;
}

if (requestCount < MaxRequestsPerSecond)
{
requestCount++;
return true;
}

return false;
}
}
}
and this is the properties for the json retrieval itself:
using Newtonsoft.Json;

namespace MBT.API
{

public partial class ScryfallApiResponse
{

[JsonProperty("data")]
public List<CardFace> Cards { get; set; }

}

public partial class CardFace
{
[JsonProperty("image_uris")]
public ImageUris ImageUrls { get; set; }
}

public partial class ImageUris
{

[JsonProperty("png")]
public Uri Png { get; set; }

}
}
using Newtonsoft.Json;

namespace MBT.API
{

public partial class ScryfallApiResponse
{

[JsonProperty("data")]
public List<CardFace> Cards { get; set; }

}

public partial class CardFace
{
[JsonProperty("image_uris")]
public ImageUris ImageUrls { get; set; }
}

public partial class ImageUris
{

[JsonProperty("png")]
public Uri Png { get; set; }

}
}
Thank you for any help in advance.
33 replies