I can't JSON to List ;_; [Answered]

The JSON value could not be converted to System.Collections.Generic.List`1[Card]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
The JSON value could not be converted to System.Collections.Generic.List`1[Card]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
That's my error Here's my attempt to just call and dump it into a List.
private static async Task ProcessRepositories()
{
var stringTask = client.GetStringAsync("https://api.magicthegathering.io/v1/cards/");
var msg = await stringTask;
var cards = JsonSerializer.Deserialize<List<Card>>(msg);
}
private static async Task ProcessRepositories()
{
var stringTask = client.GetStringAsync("https://api.magicthegathering.io/v1/cards/");
var msg = await stringTask;
var cards = JsonSerializer.Deserialize<List<Card>>(msg);
}
100 Replies
BigggMoustache
I looked at the JSON returned and it starts like this {"cards":[{"name":"Ancestor's** so I see where my issue is, but I don't know how to address it. a <List<Card> is not the same as a {"cards": arrayOfCards so I understand I should make a class for the "cards" in the json, with my existing Cards class inside of it. I'm going to try that.
mtreit
mtreit2y ago
Did you create the json by hand?
BigggMoustache
No it's return from the URL above https://api.magicthegathering.io/v1/cards/ This returns 100 cards as default and the opening {"cards":[{"name":"Ancestor's here indicates there is an object above the actual card when returning multiple correct? I don't know what it looks like when returning 1 card alone that was meant to be a question. sorry
mtreit
mtreit2y ago
Are you using Visual Studio?
BigggMoustache
yepper
mtreit
mtreit2y ago
This might help you:
BigggMoustache
what do i click on?
mtreit
mtreit2y ago
That's on the Edit menu
BigggMoustache
lol I'm misunderstanding.
mtreit
mtreit2y ago
BigggMoustache
yeah I just gotta copy the return first. still get the error using autogenerated content sec
mtreit
mtreit2y ago
Did you try deserializing to RootObject?
BigggMoustache
System.Text.Json.JsonException: 'The JSON value could not be converted to System.Collections.Generic.List`1[Rootobject]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
System.Text.Json.JsonException: 'The JSON value could not be converted to System.Collections.Generic.List`1[Rootobject]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
mtreit
mtreit2y ago
No, just RootObject Not List
BigggMoustache
oh lmao okay okay that worked. I understand a bit now. I wasn't adding Card as array in my previous class second question, if it returns one object, this form is still generally valid, or is it implementation dependent
mtreit
mtreit2y ago
Not sure what you mean
BigggMoustache
the fact that the return structure is a list of cards in this case
mtreit
mtreit2y ago
Totally depends on what was serialized
BigggMoustache
if it's one card returned it'd probably still be {"cards": oneCardInArray oh alright
mtreit
mtreit2y ago
BTW you can change Card[] to List<Card> and it should still work just fine.
BigggMoustache
yeah thanks for that too so when the json has an array built in I can't use the Deserialize<List<Object> that is what was tripping me up because EVERY tutorial on it I found was using that the deserialize multiple objects returned b ythe json
mtreit
mtreit2y ago
I don't actually work with JSON that much but I don't think you can choose to deserialize a subset of the data. I think other formats like protobuf actually support that but I'm not aware that JSON does.
BigggMoustache
well not to to a portion, just the way you tell it it's a list or array but either way I understand a bit more, and thank you very much for that.
Accord
Accord2y ago
✅ This post has been marked as answered!
mtreit
mtreit2y ago
You can serialize a List<Card> yourself to see what that looks like in JSON. The root element is a JSON array in that case. It looks like this (I removed all properties except Name for brevity)
[{"Name":"Card 0"},{"Name":"Card 1"},{"Name":"Card 2"},{"Name":"Card 3"},{"Name":"Card 4"},{"Name":"Card 5"},{"Name":"Card 6"},{"Name":"Card 7"},{"Name":"Card 8"},{"Name":"Card 9"}]
[{"Name":"Card 0"},{"Name":"Card 1"},{"Name":"Card 2"},{"Name":"Card 3"},{"Name":"Card 4"},{"Name":"Card 5"},{"Name":"Card 6"},{"Name":"Card 7"},{"Name":"Card 8"},{"Name":"Card 9"}]
BigggMoustache
Yeah I get that bud
mtreit
mtreit2y ago
Now you could take that and deserialize that to a List<Card>. But that's not the format that the web site is giving you.
BigggMoustache
so what's the point of deserialize<list<object>> right that's not the format yeah I was just approaching it the wrong way because I didn't look at the data
mtreit
mtreit2y ago
The point is that you would use that for data that is formatted like the example I gave above.
BigggMoustache
Yeah that's how the bulk data is formatted I tried that first which is why I was on this path lol couldn't get that sorted either xD. well I may just comment here for you when I try the bulk data again. thank you very much for the help. yes the return from the api was the {"cards": {...} thing I couldn't figure out how to send specific delimiters to it, but that's not really a C# question lol
mtreit
mtreit2y ago
(Invoke-WebRequest "https://api.magicthegathering.io/v1/cards").Content
(Invoke-WebRequest "https://api.magicthegathering.io/v1/cards").Content
BigggMoustache
lol why is this a bruh moment? I have 0 experience with this jsyk xD I did a specific card via \card\cardIdNumber instead of a query to get a single card and it returned JSON in the same format. Unless you meant something else. I lied actually. It is inconsistent. lol. I figured it'd automatically wrap it in an array so it could be deserialized the same way every time. Hey one more question if you don't mind. There's SDK available for this API and .net but I'm avoiding to learn more about .net and not their SDK. Does that make sense? Or should I just use the SDK and this hardship is a bit of a waste of time?
mtreit
mtreit2y ago
@rtreit probably loves this question... My twin brother. He doesn't like SDKs
BigggMoustache
lmaooo I figured it would be a valuable learning experience. Was I wrong?
mtreit
mtreit2y ago
Using the raw web calls instead of the SDK is absolutely a valuable learning experience
BigggMoustache
I'll probably take up the SDK at some point to make learning other things easier, but for now it seemed like a good choice. most awesome. Thanks for the reassurance bud. If you have suggestion for what specifically to git gud with in this regard I'm all ears xD I was just gonna pick things that looked relevant from the docs and try implementing whatever
rtreit
rtreit2y ago
^this
mtreit
mtreit2y ago
Just keep doing what you're doing I think
BigggMoustache
aight. Thanks again. blobthumbsup
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
I only used System.Text.Json for this experiment
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
oh I didn't know that was a thing thanks man
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
could you edit in the code I did? so i can see easier to compare?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
do you mean don't pass the response to a string first?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
alright will do
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
I'm not sure what you're saying so 🙂
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
lmao i'm gonna try and do that in a bit thanks again
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
yeah that makes a ton of sense
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
so i can deserialize the object that gets the json directly i didn't know that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
tbh i didn't even know what that object is because i always see it as var isn't it just a string anyways?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
I really have no idea what @tebeco is talking about but as someone who doesn't know json I assume he has some point
BigggMoustache
yeah for sure. I'm just learning it so i didn't bother doing the extras
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
I'm aware of this but have no idea how it applies to the code examples in this thread
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
hey can you write out what you mean for me?
private static async Task ProcessRepositories()
{
var stringTask = client.GetStringAsync(
"https://api.magicthegathering.io/v1/cards?name=avacyn");
var msg = await stringTask;
var cards = JsonSerializer.Deserialize<Rootobject>(msg);
}
private static async Task ProcessRepositories()
{
var stringTask = client.GetStringAsync(
"https://api.magicthegathering.io/v1/cards?name=avacyn");
var msg = await stringTask;
var cards = JsonSerializer.Deserialize<Rootobject>(msg);
}
here's the original method just so i can see what you're saying
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
oh crap lmao that's uh, a huge improvement
mtreit
mtreit2y ago
What does that have to do with utf8?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
it's a lot less data getting created and dumped for no reason
mtreit
mtreit2y ago
System.Text.Json doesn't care? I'm missing something
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
what does GetJsonAsync come from?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
nope. I saw some stuff about blazor
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
but I'm not using anything blazor googling the command
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
I guess I need to see a benchmark
BigggMoustache
well json.http isn't it xD
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
That wasn't a joke <:PES_SadGe:814280618959568896>
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
I'm supposed to be doing shoulder shrugs right now 👀
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
oh no i was just shitposting lol. I'm sitting her etrying to get this package to work instead of finishing workout
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
Just do the simplest thing possible and don't worry about micro-optimization
BigggMoustache
i added from package manager but still not working
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
not recognizing the command saved and reopened 1 sec
Severity Code Description Project File Line Suppression State
Error CS1061 'HttpClient' does not contain a definition for 'GetJsonAsync' and no accessible extension method 'GetJsonAsync' accepting a first argument of type 'HttpClient' could be found (are you missing a using directive or an assembly reference?) MTGCardDownloader C:\Users\samue\source\repos\Goofin\MTGCardDownloader\MTGCardDownloader\MTGCardDownloader\Program.cs 20 Active
Severity Code Description Project File Line Suppression State
Error CS1061 'HttpClient' does not contain a definition for 'GetJsonAsync' and no accessible extension method 'GetJsonAsync' accepting a first argument of type 'HttpClient' could be found (are you missing a using directive or an assembly reference?) MTGCardDownloader C:\Users\samue\source\repos\Goofin\MTGCardDownloader\MTGCardDownloader\MTGCardDownloader\Program.cs 20 Active
i installed from package manager and have using
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
is it because client is HttpClient?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
LMAO no error now 1 sec lmao because I read getfromjsonasyn when googling and didn't catch it derp Hot diggity that's cool man thanks a ton for telling me about that package what's BCL Oh. I had to add it 🤷🏻‍♂️ what that means, idk. lmao. yeah wasn't available to HttpClient until added unless i did something wrong which is always a possibility xD
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
✅ This post has been marked as answered!