C#
C#

help

Root Question Message

LaPetiteMortDeSam
LaPetiteMortDeSam9/20/2022
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.'

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);
        }
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
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.
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
a <List<Card> is not the same as a {"cards": arrayOfCards
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
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
mtreit9/21/2022
Did you create the json by hand?
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
No it's return from the URL above
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
https://api.magicthegathering.io/v1/cards/
This returns 100 cards as default
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
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
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
that was meant to be a question. sorry
mtreit
mtreit9/21/2022
Are you using Visual Studio?
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
yepper
mtreit
mtreit9/21/2022
This might help you:
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
what do i click on?
mtreit
mtreit9/21/2022
That's on the Edit menu
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
lol I'm misunderstanding.
mtreit
mtreit9/21/2022
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
yeah I just gotta copy the return first.
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
still get the error using autogenerated content
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
sec
mtreit
mtreit9/21/2022
Did you try deserializing to RootObject?
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
System.Text.Json.JsonException: 'The JSON value could not be converted to System.Collections.Generic.List`1[Rootobject]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
mtreit
mtreit9/21/2022
No, just RootObject
mtreit
mtreit9/21/2022
Not List
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
oh lmao
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
okay
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
okay that worked. I understand a bit now.
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
I wasn't adding Card as array in my previous class
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
second question, if it returns one object, this form is still generally valid, or is it implementation dependent
mtreit
mtreit9/21/2022
Not sure what you mean
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
the fact that the return structure is a list of cards in this case
mtreit
mtreit9/21/2022
Totally depends on what was serialized
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
if it's one card returned it'd probably still be {"cards": oneCardInArray
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
oh alright
mtreit
mtreit9/21/2022
BTW you can change Card[] to List<Card> and it should still work just fine.
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
yeah thanks for that too
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
so when the json has an array built in I can't use the Deserialize<List<Object>
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
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
mtreit9/21/2022
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.
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
well not to to a portion, just the way you tell it it's a list
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
or array
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
but either way I understand a bit more, and thank you very much for that.
mtreit
mtreit9/21/2022
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.
mtreit
mtreit9/21/2022
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"}]
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
Yeah I get that bud
mtreit
mtreit9/21/2022
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.
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
so what's the point of deserialize<list<object>>
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
right that's not the format
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
yeah I was just approaching it the wrong way because I didn't look at the data
mtreit
mtreit9/21/2022
The point is that you would use that for data that is formatted like the example I gave above.
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
Yeah that's how the bulk data is formatted
LaPetiteMortDeSam
LaPetiteMortDeSam9/21/2022
I tried that first which is why I was on this path lol
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy