C
C#2y ago
warden161

Text.Json Error

Mates I am going fucking insane. I've been working on this for a while and it's just killing me.
"[{\"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}]"
"[{\"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}]"
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
System.Text.Json.JsonSerializer.Deserialize<Player[]>(response.Content, Options);
System.Text.Json.JsonSerializer.Deserialize<Player[]>(response.Content, Options);
My options are just "PropertyNameCaseInsensitive" which is set to true.
32 Replies
Not Mark
Not Mark2y ago
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 "
warden161
warden1612y ago
There is an ending quote i just mistyped that my bad Yeah I dont know why thats occuring
Not Mark
Not Mark2y ago
How is it read in? Is it just hard coded in the same way your exampleData in the screenshot is shown?
warden161
warden1612y ago
From a REST endpoint the json thing i sent is just response's content
Not Mark
Not Mark2y ago
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 issue
warden161
warden1612y ago
When 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; } }
warden161
warden1612y ago
Not Mark
Not Mark2y ago
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?
warden161
warden1612y ago
i mean, idk, when im deserializing im making it a Player[] object unless if that doesnt work
hime
hime2y ago
What's the 495th character in the string
warden161
warden1612y ago
there doesnt seem to be 495 chars
Not Mark
Not Mark2y ago
I think it's the end of the string when I tested it
MODiX
MODiX2y ago
hime#9583
REPL Result: Failure
"[{\"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}]"[495..]
"[{\"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}]"[495..]
Exception: ArgumentOutOfRangeException
- startIndex cannot be larger than length of string. (Parameter 'startIndex')
- startIndex cannot be larger than length of string. (Parameter 'startIndex')
Compile: 368.529ms | Execution: 48.480ms | React with ❌ to remove this embed.
hime
hime2y ago
Deadge
warden161
warden1612y ago
i dont understand what the fuck is going on
hime
hime2y ago
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
warden161
warden1612y ago
yeah but that doesnt seem to exist unless if im stupid
Not Mark
Not Mark2y ago
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 json
warden161
warden1612y ago
That's a pretty good theory. I'm pretty sure you're right.
Not Mark
Not Mark2y ago
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... lol
warden161
warden1612y ago
RestSharp has a method to remove trailing quotes I believe
Not Mark
Not Mark2y ago
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
warden161
warden1612y ago
It shouldn't do that, not sure why it does.
Angius
Angius2y ago
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
warden161
warden1612y ago
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}]
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
using System.Text.Json;
var json = """
[{"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}]
""";
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; }
}
JsonSerializer.Deserialize<List<Player>>(json)
using System.Text.Json;
var json = """
[{"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}]
""";
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; }
}
JsonSerializer.Deserialize<List<Player>>(json)
Result: List<Player>
[
{
"userId": "76561198432850031@steam",
"kills": 0,
"deaths": 0,
"experiencePoints": 0,
"lastSeen": "2022-12-27T00:31:32.738454",
"firstSeen": "2022-12-26T22:31:43.768427",
"colaDrinks": 0,
"medkits": 0,
"adrenalineShots": 0,
"pocketEscapes": 0,
"roundsPlayed": 6,
"doNotTrack": false
}
]
[
{
"userId": "76561198432850031@steam",
"kills": 0,
"deaths": 0,
"experiencePoints": 0,
"lastSeen": "2022-12-27T00:31:32.738454",
"firstSeen": "2022-12-26T22:31:43.768427",
"colaDrinks": 0,
"medkits": 0,
"adrenalineShots": 0,
"pocketEscapes": 0,
"roundsPlayed": 6,
"doNotTrack": false
}
]
Compile: 583.543ms | Execution: 61.425ms | React with ❌ to remove this embed.
Angius
Angius2y ago
¯\_(ツ)_/¯ Works fine
warden161
warden1612y ago
yeah restsharp seems to be fucking it up with however its getting the string
Angius
Angius2y ago
Don't use RestSharp Just use HttpClient
var data = await _client.GetFromJsonAsync<Player[]>(yourUrl);
var data = await _client.GetFromJsonAsync<Player[]>(yourUrl);
boom done
warden161
warden1612y ago
how does one add a header to this
Angius
Angius2y ago
_client.DefaultRequestHeaders.Add("Foo", "Bar");
_client.DefaultRequestHeaders.Add("Foo", "Bar");
for example
warden161
warden1612y ago
thanks!