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
BigggMoustacheOP3y ago
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
mtreit3y ago
Did you create the json by hand?
BigggMoustache
BigggMoustacheOP3y ago
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
mtreit3y ago
Are you using Visual Studio?
BigggMoustache
BigggMoustacheOP3y ago
yepper
mtreit
mtreit3y ago
This might help you:
BigggMoustache
BigggMoustacheOP3y ago
what do i click on?
mtreit
mtreit3y ago
That's on the Edit menu
BigggMoustache
BigggMoustacheOP3y ago
lol I'm misunderstanding.
mtreit
mtreit3y ago
BigggMoustache
BigggMoustacheOP3y ago
yeah I just gotta copy the return first. still get the error using autogenerated content sec
mtreit
mtreit3y ago
Did you try deserializing to RootObject?
BigggMoustache
BigggMoustacheOP3y ago
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
mtreit3y ago
No, just RootObject Not List
BigggMoustache
BigggMoustacheOP3y ago
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
mtreit3y ago
Not sure what you mean
BigggMoustache
BigggMoustacheOP3y ago
the fact that the return structure is a list of cards in this case
mtreit
mtreit3y ago
Totally depends on what was serialized
BigggMoustache
BigggMoustacheOP3y ago
if it's one card returned it'd probably still be {"cards": oneCardInArray oh alright
mtreit
mtreit3y ago
BTW you can change Card[] to List<Card> and it should still work just fine.
BigggMoustache
BigggMoustacheOP3y ago
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
mtreit3y 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
BigggMoustacheOP3y ago
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
Accord3y ago
✅ This post has been marked as answered!
mtreit
mtreit3y 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
BigggMoustacheOP3y ago
Yeah I get that bud
mtreit
mtreit3y 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
BigggMoustacheOP3y ago
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
mtreit3y ago
The point is that you would use that for data that is formatted like the example I gave above.
BigggMoustache
BigggMoustacheOP3y ago
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
mtreit3y ago
(Invoke-WebRequest "https://api.magicthegathering.io/v1/cards").Content
(Invoke-WebRequest "https://api.magicthegathering.io/v1/cards").Content
BigggMoustache
BigggMoustacheOP3y ago
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
mtreit3y ago
@rtreit probably loves this question... My twin brother. He doesn't like SDKs
BigggMoustache
BigggMoustacheOP3y ago
lmaooo I figured it would be a valuable learning experience. Was I wrong?
mtreit
mtreit3y ago
Using the raw web calls instead of the SDK is absolutely a valuable learning experience
BigggMoustache
BigggMoustacheOP3y ago
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
rtreit3y ago
^this
mtreit
mtreit3y ago
Just keep doing what you're doing I think
BigggMoustache
BigggMoustacheOP3y ago
aight. Thanks again. blobthumbsup
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
I only used System.Text.Json for this experiment
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
oh I didn't know that was a thing thanks man
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
could you edit in the code I did? so i can see easier to compare?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
do you mean don't pass the response to a string first?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
alright will do
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
I'm not sure what you're saying so 🙂
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
lmao i'm gonna try and do that in a bit thanks again
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
yeah that makes a ton of sense
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
so i can deserialize the object that gets the json directly i didn't know that
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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 User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y 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
BigggMoustacheOP3y ago
yeah for sure. I'm just learning it so i didn't bother doing the extras
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
I'm aware of this but have no idea how it applies to the code examples in this thread
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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 User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
oh crap lmao that's uh, a huge improvement
mtreit
mtreit3y ago
What does that have to do with utf8?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
it's a lot less data getting created and dumped for no reason
mtreit
mtreit3y ago
System.Text.Json doesn't care? I'm missing something
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
what does GetJsonAsync come from?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
nope. I saw some stuff about blazor
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
but I'm not using anything blazor googling the command
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
I guess I need to see a benchmark
BigggMoustache
BigggMoustacheOP3y ago
well json.http isn't it xD
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
That wasn't a joke <:PES_SadGe:814280618959568896>
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
I'm supposed to be doing shoulder shrugs right now 👀
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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 User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
Just do the simplest thing possible and don't worry about micro-optimization
BigggMoustache
BigggMoustacheOP3y ago
i added from package manager but still not working
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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
BigggMoustache
BigggMoustacheOP3y ago
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
is it because client is HttpClient?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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 User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server