C
C#7mo ago
Nesli

Newtonsoft.Json json handling

i got a message like "Rooms":{"room1", "room2"} how can i deserialize such message? preferably put it in some sort of array/list
13 Replies
Nesli
NesliOP7mo ago
internal class RoomData
{
public List<string> Rooms { get; set; }
}
internal class RoomData
{
public List<string> Rooms { get; set; }
}
tried making this class to hold the data but jsonConvert.Deserialize (i dont remember the full command lol) just crashes
SleepWellPupper
SleepWellPupper7mo ago
The message you posted is not valid json. If you do indeed have json, you should use System.Text.Json and consult this page for an easy way in VS to create the target class: https://learn.microsoft.com/en-us/visualstudio/ide/reference/paste-json-xml?view=vs-2022
Paste JSON or XML as classes - Visual Studio (Windows)
Learn how to copy any JSON or XML text from the clipboard and then paste it as .NET classes into C# or Visual Basic code.
SleepWellPupper
SleepWellPupper7mo ago
Then use System.Text.Json.Serializer.Deserialize<RoomData>(jsonString)
Nesli
NesliOP7mo ago
i mightve not read it correctly, let me re-run it
SleepWellPupper
SleepWellPupper7mo ago
Newtonsoft.Json was obsoleted by System.Text.Json and should not be used in new projects.
Nesli
NesliOP7mo ago
aw nahh my partner fucked up the serialization in c++ 😭
No description
Nesli
NesliOP7mo ago
well i guess i can make Rooms a string and just split it
Nesli
NesliOP7mo ago
okay its working nevermind
No description
Nesli
NesliOP7mo ago
thank you for making me check lmao
The Fog from Human Resources
Newtonsoft.Json is not recommended anymore as the default json class in C# is already more than capable
Mustafa
Mustafa7mo ago
As the people above have already stated, switch to System.Text.Json. After that, you can incorporate something similar to this:
string JsonContent = File.ReadAllText("put file path here");
var deserializedContent = JsonSerializer.Deserialize<RoomData>(JsonContent);
string JsonContent = File.ReadAllText("put file path here");
var deserializedContent = JsonSerializer.Deserialize<RoomData>(JsonContent);
Nesli
NesliOP7mo ago
yeah lol, i didnt read the text i was getting correctly ive fixed it though
Mustafa
Mustafa7mo ago
👍
Want results from more Discord servers?
Add your server