Text.Json Error
Mates I am going fucking insane. I've been working on this for a while and it's just killing me.
I've got this json here.
It's giving me this exception:
"System.Text.Json.JsonException: The JSON value could not be converted to StatsBot.JsonHelper+Player[]. Path: $ | LineNumber: 0 | BytePositionInLine: 495.
For some reason, when I use a different json (attached in image) it works.
The code I'm using is
My options are just "PropertyNameCaseInsensitive" which is set to true.32 Replies
This might just be a copy-paste thing, but is there an end quote on the original json? Where are you reading the original json from? I copied the original and replaced the \" with just a " and it seems like valid json ignoring the starting "
There is an ending quote
i just mistyped that my bad
Yeah I dont know
why thats occuring
How is it read in? Is it just hard coded in the same way your
exampleData
in the screenshot is shown?From a REST endpoint
the json thing i sent is just response's content
Can you do me a favor and copy and paste your
Player
model? I'm just trying to test it on my end to replicate your issueWhen I tried it in #bot-spam it returned that
Yeah one sec I just have to remove the attribtues
public class Player
{
public string UserId { get; set; }
public int Kills { get; set; }
public int Deaths { get; set; }
public int ExperiencePoints { get; set; }
public string LastSeen { get; set; }
public string FirstSeen { get; set; }
public int ColaDrinks { get; set; }
public int Medkits { get; set; }
public int AdrenalineShots { get; set; }
public int PocketEscapes { get; set; }
public int RoundsPlayed { get; set; }
public bool DoNotTrack { get; set; }
}
well that specific case you can't because the json represents a list of players
But I think that's not the original issue you had?
i mean, idk, when im deserializing im making it a Player[] object unless if that doesnt work
What's the 495th character in the string
there doesnt seem to be 495 chars
I think it's the end of the string when I tested it
hime#9583
REPL Result: Failure
Exception: ArgumentOutOfRangeException
Compile: 368.529ms | Execution: 48.480ms | React with ❌ to remove this embed.
i dont understand what the fuck is going on
The error message
It says it's in line 0 position 495
That's where the problem starts
So I asked for the character at that position
yeah but that doesnt seem to exist
unless if im stupid
What I'm guessing is what is happening is it's trying to deserialize
"[{your_object}]"
as a list of players instead of just [{your_object}]
as a list of players.
So it's taking a string representing a string literal which represents your json instead of just taking the string of your jsonThat's a pretty good theory. I'm pretty sure you're right.
I guess there are several ways to handle this. One way is to read the string
response.Content
and manually remove the " quotes at the beginning and end, and then deserialize. You could also deserialize response.Content
into a string first and then deserialize that string into your Player[]
. I just tested this way and it seems to work. I don't know if that's the best way of handling that though... lolRestSharp has a method to remove trailing quotes I believe
You could try that. I find it odd though that the API you're interacting with is giving quotes wrapping the whole json in the response though
It shouldn't do that, not sure why it does.
Call the API with some REST client and see what the response is
Hell, open the URL in the browser if it doesn't need any special auth
it does but it does not return that, it returns it fine
[{"Id":732870,"UserId":"76561198432850031@steam","LastSeen":"2022-12-27T00:31:32.738454","FirstSeen":"2022-12-26T22:31:43.768427","Activity":{"2022-12-26T00:00:00Z":3209.0,"2022-12-27T00:00:00Z":1894.0},"DoNotTrack":false,"Staff":true,"Kills":0,"Deaths":0,"ExperiencePoints":0,"ColaDrink":1,"Medkits":0,"AdrenalineShots":0,"Survival":0,"PocketEscapes":0,"RoundsPlayed":6,"Teamkills":0,"EscapeTimes":[],"Level":0,"Name":null,"IgnoreStats":0}]
Angius#1586
REPL Result: Success
Result: List<Player>
Compile: 583.543ms | Execution: 61.425ms | React with ❌ to remove this embed.
¯\_(ツ)_/¯
Works fine
yeah
restsharp seems to be fucking it up with however its getting the string
Don't use RestSharp
Just use HttpClient
boom
done
how does one add a header to this
for example
thanks!